The Artima Developer Community
Sponsored Link

Java Answers Forum
Socket programming with Threads

1 reply on 1 page. Most recent reply: Jan 14, 2003 10:28 PM by Sarma K T R B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Harish

Posts: 1
Nickname: hari908
Registered: Jan, 2003

Socket programming with Threads Posted: Jan 14, 2003 4:41 AM
Reply to this message Reply
Advertisement
Hi
I am implementing Client Socket architecture.i need to use one
Thread for Transmitting and one Thread for Receving.but i can't make them act simultaniously.please help me.


Sarma K T R B

Posts: 5
Nickname: sarma2312
Registered: Jan, 2003

Re: Socket programming with Threads Posted: Jan 14, 2003 10:28 PM
Reply to this message Reply
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();
}
}

Flat View: This topic has 1 reply on 1 page
Topic: harddisk selector Previous Topic   Next Topic Topic: Error in accessing the javax package

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use