Archive

Archive for the ‘Others’ Category

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):


Java Program to show implement Low & High priority Threads – Q27

January 14, 2009 Leave a comment

Q27: Java Program to create clicker class which sets the priority of two threads (one is having high priority and one is having low priority) and whenever the thread is running it is increasing a counter. In the end print what is the value of the counter for both of the threads.

class clicker implements Runnable{
	int click=0;
	Thread t;
	private volatile boolean running=true;
	
	public clicker(int p){
		t = new Thread(this);
		t.setPriority(p);
	}
	
	public void run(){
		while(running){
			click++;
		}
	}
	
	public void stop(){
		running=false;
	}
	
	public void start(){
		t.start();
	}
}

class HiLoPri{

	public static void main(String args[]){
		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		clicker hi = new clicker(Thread.NORM_PRIORITY+2);
		clicker lo = new clicker(Thread.NORM_PRIORITY-2);
		
		lo.start();
		hi.start();
		
		try{
			Thread.sleep(1000);
		}
		catch(InterruptedException e){
			System.out.println("Main thread interrupted.");
		}
		
		lo.stop();
		hi.stop();
		
		try{
			hi.t.join();
			lo.t.join();
		}
		catch(InterruptedException e){
			System.out.println("InterruptedException caught");
		}

		System.out.println("Low-Prioritythread value: " + lo.click);
		System.out.println("High-Prioritythread value: " + hi.click);
	}
}

… from College notes (BCA/MCA assignments):


Java Program to show Runnable Interface implementation – Q26

January 13, 2009 Leave a comment

Q26: Java Program to create thdemo class which implements Runnable Interface. Main thread prints numbers from 1 to 10 and child thread prints from 1 to 5.

A Runnable Interface should be implemented by any class whose instances are intended to be executed by a Thread. The class must define a method of no arguments called run.

class NewThread implements Runnable{
	
	Thread t;
	
	NewThread(){
		t = new Thread(this, "Demo Thread");
		System.out.println("Child thread: " + this);
		t.start();
	}
	
	public void run(){
		try{
			for(int i=1; i<=5; i++){
				System.out.println("Child Thread: " + i);
				System.out.println("Child Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("Exiting child thread");
		}
	}
}	

class demo_thread_runnable{

	public static void main(String args[]){
		new NewThread();
		try{
			for(int i=1; i<=10; i++){
				System.out.println("Main Thread: " + i);
				System.out.println("Main Thread interrupted.");
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e){
			System.out.println("main thread interrupted.");
		}
	System.out.println("Main Thread exiting");
	}
}

… from College notes (BCA/MCA assignments):