Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multi Thread

Posted on 2002-04-03
3
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 400 total points
ID: 6917413
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
ID: 6917416
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
ID: 6919633
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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