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
Solved

Multi Thread

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Maven Project: Hibernate Dependencies Conflict 10 40
login form jsp example 2 37
Running JavaFX on the Raspberry Pi 27 45
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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