Archive

Archive for April, 2010

PingPong in Java

April 11, 2010 4 comments

While I was trying to learn Scala Actors, I came across PingPong problem. It is the simplest form of two different threads communicating each other. For Scala version checkout here – http://www.scala-lang.org/node/54. Below is the pingpong in java with two different implementation. One using locks and other one using classic thread programming.

package test;

public class PingPong {
	private final int MAX = 10;
	private volatile int counter = 0;
	private volatile boolean ponged = true;

	public synchronized void ping() {
		try {
			if (ponged) {
				counter++;
				System.out.println("Ping");
				ponged = false;
			}
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized void pong() {
		if (!ponged) {
			counter++;
			System.out.println("Pong");
			ponged = true;
		}
		notifyAll();
	}

	public static void main(String... arg) {
		final PingPong pingpong = new PingPong();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.ping();
			}
		}.start();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.pong();
			}
		}.start();
	}
}
package test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PingPongLock {
	private final int MAX = 10;
	private volatile int counter = 0;
	private Lock lock = new ReentrantLock();
	private Condition pinged;
	private Condition ponged;

	public PingPongLock() {
		pinged = lock.newCondition();
		ponged = lock.newCondition();
	}

	public void ping() {
		try {
			lock.lock();
			counter++;
			System.out.println("Ping");
			pinged.signalAll();
			if (counter < MAX)
				ponged.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void pong() {
		try {
			lock.lock();
			counter++;
			System.out.println("Pong");
			ponged.signalAll();
			if (counter < MAX)
				pinged.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public static void main(String... arg) {
		final PingPongLock pingpong = new PingPongLock();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.ping();
			}
		}.start();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.pong();
			}
		}.start();
	}
}

Advertisements
Categories: Uncategorized

Date, TimeZone, Calendar – converter in Java

April 5, 2010 Leave a comment

have enough wasted working with java.util.Time(Sun named it as java.util.Date), and SimpleDateFormat(UnSpecifiedTimeZoneAndByDefaultJVMTimeZone).

Whenever dealing with Date object remember that your working with time. If you need to pass different timezones of date then it is named as Calendar in java. So pass Calendar and not date. That is what I learn after struggling with it for hours.

<pre>
package com.nikias.calendar;

import java.util.Calendar;
import java.util.TimeZone;

/*
 * Helps to print current time in GMT, local JVM, Indian Time, Singapore Time, New-york Time
 *
 * America/New_York
 * Asia/Calcutta
 * Asia/Singapore
 * GMT
 *
 */
public class TimeZoneConverter {

 public TimeZoneConverter() {
 }

 public static void main(String[] args) {
 TimeZoneConverter converter = new TimeZoneConverter();
 String[] timeZones = new String[] { "Asia/Calcutta", "America/New_York", "Asia/Singapore", "GMT" };

 for (String tz : timeZones) {
 System.out.printf(tz + " --    %tc\n", converter.fromGMT(Calendar.getInstance(), tz));
 }
 }

 public Calendar fromGMT(Calendar sourceTime, String targetZone) {
 return fromGMT(sourceTime, TimeZone.getTimeZone(targetZone));
 }

 public Calendar fromGMT(Calendar sourceTime, TimeZone targetZone) {
 Calendar target = Calendar.getInstance(targetZone);
 target.setTime(sourceTime.getTime());
 return target;
 }
}

Below is the unit testcase to prove them

package com.nikias.calendar;

import java.util.Calendar;
import java.util.TimeZone;

import junit.framework.Assert;

import org.junit.Test;

public class TimeZoneConverterTest {

	// Sunday, January 31st, 8:00AM

	long day = 24 * 60 * 60 * 1000;
	long year = (long) day * 366;
	long time = (long) year * (2010 - 1970);
	long dsttime = (long) year * (2010 - 1970) + 93*day;
	long dsttime2 = (long) year * (2010 - 1970) + 90*day;

	@Test
	public void testSingaporeTime() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(time);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(time);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH), calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH) + 1, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 11);
	}

	@Test
	public void testDSTAndSingaporeTime() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(dsttime);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(dsttime);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH), calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH) + 1, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 12);
	}
	
	@Test
	public void testDSTAndSingaporeTimeAndEndOfMonth() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(dsttime2);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(dsttime2);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH) + 1, calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH)-29, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 12);
	}	

	
}
Categories: Uncategorized