|
Re: Socket programming with Threads
|
Posted: Jan 14, 2003 10:28 PM
|
|
hi, When you create multiple threads and start both the threads, they will be simultaneously started, so by using Thread.currentThread().getName() you can find the current thread and by manipulating in an if else loop you can do the job as one thread will be the receiving messages and other will send messages I am pasting the code below modify the code according to your requirements.
Hope this helps you.
public class ThreadTest implements Runnable { private volatile Thread th1=new Thread(this); private volatile Thread th2=new Thread(this);
public ThreadTest() { //set name to threads
th1.setName("th1"); th2.setName("th2"); th1.start(); th2.start(); }
public void run() { while(true){ System.out.println(" Current Thread Running is: "+Thread.currentThread().getName()); try{ Thread.currentThread().sleep(2000); }catch(InterruptedException ie){ } } }
public static void main(String a[]) { ThreadTest tt=new ThreadTest(); } }
|
|