CS 342 - 5/1/14 Singleton Pattern class MyS { private volatile static MyS x = null; private Object data; private MyS () { // do initialization as needed data = new Object(); } public static MyS getInstance() { if (x == null) { synchronized (MyS.class) { if (x == null) x = new MyS(); } } return x; } public Object getData() { return data; } public void setData (Object d) { data = d; } public .... other methods ... } methodZ { MyS s1 = MyS.getInstance(); z = s1.getData(); } methodY { MyS s2 = MyS.getInstance(); y = s2.getData(); } --------------------- Threads start() run() Create an instance of the Thread() of something that inherits from Thread() class MyThread extends Thread() { static int shared; Object s; int value; public MyThread (int v, Object o) { value = v; s = o; //start(); } public void run() { System.out.println ("New Thread with value: " + value ); .... if (...) return; ..... } } // in some other method Object o = new Object(); MyThread x1 = new MyThread(5, o); MyThread x2 = new MyThread(7, o); x1.start(); System.out.println ("Original Thread"); -------------- Sockets focus on connection section Server side Client side ------------- ------------- create ServerSocket instance accept() method on ServerSocket create Socket instance using machine info and port info of the serverSocket accept() method returns with an instance of socket for the server side to use use socket instance to commincation use socket instance to with client comminucate with server ----- Multiple simultaneous clients to the same server The accept() code nededs to be put in a loop that spawns a new thread for each client connection. The threads must be given the Socket instance that was returned by accept()