Less than 42 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

starting multiple threads  RSS feed

 
Vrush
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I want to start multiple threads after a certain time interval. I tried soemthing like this..
I am giving a start command in a for loop. But its not going in Run method..
Pls help

/**
* Insert the type's description here.
* Creation date: (1/11/2002 9:25:07 AM)
* @author: Administrator
*/
class temothread implements Runnable {

static Thread qThread[]=new Thread[5];
static Thread myThread;
/**
* temothread constructor comment.
*/
public temothread() {
super();
}
public void run()
{
//myThread=Thread.currentThread();
System.out.println("thread running "+myThread.getName());
sleepThread();
}
public static void sleepThread()
{
if(myThread != null)
try{
myThread.sleep(1000);
}
catch(Exception e)
{
System.out.println("WException occured while sleeping "+e.getMessage());
}
}
public static void main(String[] args)
{

for(int i=0;i<2;i++)
{
qThread[i]=new Thread("Thread"+i);
myThread=qThread[i];
System.out.println("starting thread "+i);
sleepThread();
myThread.start();
}

}
public static void sleepThread(Thread thisThread)
{
if(thisThread != null)
{
try{
thisThread.sleep(1000);
}
catch(Exception e)
{
System.out.println("WException occured while sleeping "+e.getMessage());
}
}
}}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to initialize each thread with
a Runnable instance. The new thread will
call the run method of the Runnable
instance.
The line inside the for loop should look
something like this:
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more specific, the following should fix your code:
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, sleep() is a static method of the Thread class. If you were to replace myThread.sleep( 1000 ) with Thread.sleep( 1000 ), the calling thread (in this case, the main thread) would wait 1 second before calling the start method of each new thread instance in each iteration of the for loop.
If you wish for the new thread to wait one second after is it started to execute its run method, place the Thread.sleep( 1000 ) call as the first line of the run method.
 
Alex Zhang
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an alternative way to write thread is to extends thread instead of implements runnable
Sample:
public class CustomThread extends Thread {
/* code */
}
so U can directly instantiate it by
CustomThread myThread = new CustomThread();
and start it with
myThread.start();
hope that this can clarify something
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!