Solved

Multi Thread

Posted on 2002-04-03
3
209 Views
Last Modified: 2010-03-31
Hi guys,

I am writing a program that will use TCP to communicate to other program, and everything is working just fine. now I want to add second Thread into the my client class, and I am confused.
here is the snap shot of my existing code.

public class Client implements Runnable
{
protected Thread myThread;
public void start()
{
if(myThread==null) myThread=new Thread(this);
myThread.start();
}
public void stop(){... do something}
public void run(){
 while (myThread!=null)
 {
    talking to other program through TCPIP
  }
}

now I want to add second thread called timer into this class, is it doable?
if so, how does it work?

Thanks
0
Comment
Question by:cgoca
3 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
Comment Utility
Hi,

First of all, you could use java.util.Timer, it will handle threads for you.

You cannot create thread for different task using same implementation of Runnable (you need different run), so write another class TimerClient implements Runnable and create thread for it.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1

 
0
 

Expert Comment

by:rashidkamranbs
Comment Utility
Its better to Make Threads as seprate classes Like Timer etc and in Client Class declare as many object as you need and every thread would be responsible to its operations..

I feel its not good to directly implementa runnable..

it could be

class WhatEverYouLike extends Thread{
   public void run(){
     //do something for whatever it is supposed to
   }
}

class Timer extends Thread{
  public void run(){
    //do something for timer
  }
}

class Client{
  protected WhatEverYouLike thread1;
  protected Timer thread2;
  //now you can write methods in the client to do the job
}

A raw idea.. rest ya can do urself.. it may help you in your client application..
0
 
LVL 2

Author Comment

by:cgoca
Comment Utility
I am totally understand the method of extend thread class.
say we have a class A implement runnable,
and in class A we will have a lot of object which is extend from Thread?
or class A will only be able to have just one object then have many instance of class A?

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now