[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multi Thread

Posted on 2002-04-03
3
Medium Priority
?
226 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 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