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/

1 comment:

  1. AnonymousJune 04, 2013

    It's fantastic that you are getting ideas from this piece of writing as well as from our discussion made here.

    My web page - http://www.dostave.com

    ReplyDelete

Your feedback may help others !!!

Facebook comments