Showing posts with label java thread example. Show all posts
Showing posts with label java thread example. Show all posts

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/

October 23, 2012

Java client server socket program example

Java networking program to get client ip address in server.


Description : The client tries to establish a connection to server having ip address 127.0.0.1 through port 6363. The socket class is used to establish connection . The server always listen to port 6363 for request from the clients. Once a request reach the server through that port, it establish a connection with that particular client. Multiple clients can connect to server, since i used thread in server.

Facebook comments