Saturday, June 9, 2007
Commanding Threads in Java
Following are source to command threads in java
Command.java
package tekzgeek.thread;
public class Command
{
public static int PAUSE=0;
public static int RESUME=1;
public static int EXIT=2;
int state=RESUME;
int command=RESUME;
synchronized public int getCommand()
{
if(this.state==this.command)
return this.state;
if((this.state==RESUME)&&(this.command==PAUSE))
{
this.state=PAUSE;
notify();
while((this.command!=Command.RESUME)&&(this.command!=Command.EXIT))
{
try
{
wait();
}
catch(InterruptedException e)
{}
}
if(this.command==Command.RESUME)
this.state=Command.RESUME;
else if(this.command==Command.EXIT)
this.state=Command.EXIT;
}
else if((this.state==Command.RESUME)&&(this.command==Command.EXIT))
{
this.state=Command.EXIT;
notify();
}
return this.state;
}
synchronized public void setCommand(int command_temp)
{
this.command=command_temp;
if(state==command)
return;
if((this.state==Command.RESUME)&&(this.command==Command.PAUSE))
{
while(this.state!=Command.PAUSE)
{
try
{
wait();
}
catch(InterruptedException e)
{}
}
}
else if((state==PAUSE)&&(command==RESUME))
{
notify();
}
else if((this.state==Command.RESUME)&&(this.command==Command.EXIT))
{
while(this.state!=Command.EXIT)
{
try
{
wait();
}
catch(InterruptedException e)
{}
}
}
else if((this.state==Command.PAUSE)&&(this.command==Command.EXIT))
{
notify();
}
}
}
WorkerThread.java
package tekzgeek.thread;
import tekzgeek.thread.Command;
public class WorkerThread extends Thread
{
Command command;
public WorkerThread(Command command)
{
this.command=command;
}
public void run()
{
boolean EXIT=false;
while(!EXIT)
{
// Do Some Work
System.out.println("Worker Thread Running.");
try
{
Thread.sleep(200);
}catch(InterruptedException e)
{}
int cmd=command.getCommand();
if(cmd==Command.EXIT)
EXIT=true;
}
System.out.println("I am done.");
}
}
Main.java
package tekzgeek.thread;
import tekzgeek.thread.Command;
import tekzgeek.thread.WorkerThread;
public class Main
{
public static void main(String args[])throws InterruptedException
{
Command command=new Command();
WorkerThread workerThread=new WorkerThread(command);
workerThread.start();
Thread.sleep(1000);
System.out.println("Main Thread is trying to Pause Worker Thread.");
command.setCommand(Command.PAUSE);
System.out.println("Main Thread Paused Worker Thread.");
Thread.sleep(1000);
System.out.println("Main Thread is trying to Resume Worker Thread.");
command.setCommand(Command.RESUME);
System.out.println("Main Thread Resumed Worker Thread.");
Thread.sleep(1000);
System.out.println("Main Thread is trying to Stop Worker Thread.");
command.setCommand(Command.EXIT);
System.out.println("Main Thread stopped Worker Thread.");
}
}
Command.java- The basic core for transfering messages between threads.
Main.java- Main Thread that commands Worker Thread.
WorkerThread.java- This thread does the work and is controlled by Main Thread.
pl note : One way command is only implemented. To go for two way communication or for feedback,create
one more command for getting feedback from worker thread.
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment