Solved

Multi Thread

Posted on 2002-04-03
3
211 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 38
diffSum example 4 37
tomcat administrtor 12 47
How do I remove an object from a 3 25
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

773 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