Advertisements

Archive

Posts Tagged ‘Thread Synchronization’

Java Program to show Thread Synchronization by user defined Get & Put methods – Q31

January 16, 2009 Leave a comment

Q31: Java Program to create Q class having Get and Put methods and then create Producer and Consumer classes which accept Q object in their constructor. Put and get the numbers through a for loop in producer and consumer, use an array to put and get the numbers.

class Q{
	int n;

	synchronized int get(){
		System.out.println("Get: " + n);
		return n;
	}

	synchronized void put(int n){
		this. n = n;
		System.out.println("Put: " + n);
	}
}

class Producer implements Runnable{
	Q q;

	Producer(Q q){
		this.q = q;
		new Thread(this, "Producer").start();
	}

	public void run(){
		int i=0;
		while(true){
			q.put(i++);
		}
	}
}

class Consumer implements Runnable{
	Q q;

	Consumer(Q q){
		this.q = q;
		new Thread(this, "Consumer").start();
	}

	public void run(){
		while(true){
			q.get();
		}
	}
}

class PC{

	public static void main(String args[]){
		Q q = new Q();
		new Producer(q);
		new Consumer(q);
		
		System.out.println("Press Ctrl-C to STOP");
	}
}

… from College notes (BCA/MCA assignments):


Advertisements

Java Program to handle Thread Synchronization by using synchronized object – Q30

January 15, 2009 Leave a comment

Q30: Java Program to synchronize methods through synchronizing object

class Callme{
	void call(String msg){
		System.out.print("[" + msg);
		try{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
		System.out.println("]");
	}
}

class Caller implements Runnable{
	String msg;
	Callme target;
	Thread t;
	
	public Caller(Callme targ, String s){
		target = targ;
		msg = s;
		t = new Thread(this);
		t.start();
	}
	public void run(){
		synchronized(target){
			target.call(msg);
		}
	}
}

class Synch1{
	public static void main(String args[]){
		Callme target = new Callme();
		Caller ob1 = new Caller(target, "Hello");
		Caller ob2 = new Caller(target, "Dear");
		Caller ob3 = new Caller(target, "Manoj");
		try{
			ob1.t.join();
			ob2.t.join();
			ob3.t.join();
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program to handle Thread Synchronization – Q29

January 15, 2009 Leave a comment

Q29: Java Program to synchronize methods in the previous Program and get the correct output.

class Callme{
	synchronized void call(String msg){
		System.out.print("[" + msg);
		try{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
		System.out.println("]");
	}
}

class Caller implements Runnable{
	String msg;
	Callme target;
	Thread t;
	
	public Caller(Callme targ, String s){
		target = targ;
		msg = s;
		t = new Thread(this);
		t.start();
	}
	
	public void run(){
		target.call(msg);
	}
}

class Synch{
	public static void main(String args[]){
		Callme target = new Callme();
		Caller ob1 = new Caller(target, "Hello");
		Caller ob2 = new Caller(target, "Dear");
		Caller ob3 = new Caller(target, "Manoj");
		try{
			ob1.t.join();
			ob2.t.join();
			ob3.t.join();
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program to show Thread Synchronization – Q28

January 14, 2009 Leave a comment

Q28: Java Program to show that if three threads are of equal priority and they are not synchronized then more than one thread may be executing in single function which give wrong values. Output ([hello[dear[bhaskar]]]).

class Callme{
	void call(String msg){
		System.out.print("[" + msg);
		try{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
		System.out.println("]");
	}
}

class Caller implements Runnable{
	String msg;
	Callme target;
	Thread t;
	public Caller(Callme targ, String s){
		target = targ;
		msg = s;
		t = new Thread(this);
		t.start();
	}
	public void run(){
		target.call(msg);
	}
}

class Synch{
	public static void main(String args[]){
		Callme target = new Callme();
		Caller ob1 = new Caller(target, "Hello");
		Caller ob2 = new Caller(target, "Dear");
		Caller ob3 = new Caller(target, "Manoj");
		try{
			ob1.t.join();
			ob2.t.join();
			ob3.t.join();
		}
		catch(InterruptedException e){
			System.out.println("Interrupted");
		}
	}
}

… from College notes (BCA/MCA assignments):