January 29, 2013

Thread Enumerator Example

Java example to set condition in Thread 


package yahoofinance;

public class ThreadExamples implements Runnable {
   
    public enum Traffic { NORTH, SOUTH, EAST, WEST, NOTHING }
    static Traffic isOn = Traffic.NOTHING;
    @Override
    public void run() {
        while(true){
            switch (isOn) {
            case NORTH:
                System.out.println(" north is on ");
                break;
            case SOUTH:
                System.out.println(" south is on ");
                break;
            case EAST:
                System.out.println(" east is on ");
                break;
            case WEST:
                System.out.println(" west is on ");
                break;
            case NOTHING:
                System.out.println(" default is on ");
                break;
            default:
                System.out.println(" default is on ");
                break;
            }
        }
       
    }
   
    public static void main(String[] election) throws InterruptedException {
         ThreadExamples t = new ThreadExamples();
         Thread th = new Thread(t);
         th.start();
         th.sleep(5000);
         isOn = Traffic.NORTH;
         th.sleep(1000);
         isOn = Traffic.WEST;
    }

}

How to manipulate/control a running thread from another class.



http://belazy.blog.com/

Facebook comments