Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single thread that runs 5 times, sleeps, then runs 5 more times

 
Sandra Waller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am new to Java and I am having problems figuring out how to instantiate and create a single thread. The thread must run 5 times, sleep for 2 seconds, then run 5 more times. Here is what I am trying to do, but the compiler doesn't like the way I've created the thread:
public class Thread1 extends Thread
{
protected thread = new Thread("I am the main thread", 10);
//I get an error here
// Main method
public static void main(String[] args)
{
//Create the thread
Thread thread = new Thread();
//I get an error here
//Create a loop to print out the thread 5 times
for(int i = 0; i < 5; i++)
System.out.println(thread);
try
{
//Put the thread to sleep for 2 seconds
thread.sleep(2000);
}
catch (InterruptedException e)
{
}

//Create a loop to print out the thread 5 more times
for(int j = 0; j < 5; j++)
System.out.println(thread);
}
}
Any help is greatly appreciated and thank you for your time
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, bit of confusion in your code. I'd start by designing your Thread object and running it from code in a separate class. The "main" method has nothing to do with Threads.
(sorry I can't finish it, I'm a bit short on time, but someone will pick this up soon )
Dave
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandra,
I've recently started looking into threads and find them fascinating. Lots of ways to do what you want here. Here's one revised version. (At some stage, you might also investigate implementing the Runnable interface instead of extending the Thread class. It's a little more complex, but it enables you to have you application extend another class.)
Comments on your original code follow.
cheerio
rowan
~~~~~~~~~~~
public class Thread1 extends Thread
{
//declare thread
private Thread myThread;
//variable for the amount of time the thread is to sleep
private int sleepTime;
//variable for the thread name
private String threadName;

//constructor for Thread takes a user defined thread name and
//sleep period
public Thread1(String threadName, int sleepTime){
this.threadName = threadName;
this.sleepTime = sleepTime;
myThread = new Thread(threadName);
myThread.start();
loop(myThread);
sleep(myThread, sleepTime);
loop(myThread);
}

//A loop method avoids having to code this twice
private void loop(Thread looper){
//Create a loop to print out the thread 5 times
for(int i = 0; i < 5; i++)
System.out.println(looper.getName());
}

private void sleep(Thread sleeper, int sleepTime){
try{
System.out.println(sleeper.getName() + " just about to sleep");
sleeper.sleep(sleepTime);
}
catch(InterruptedException e){
}
}

//Main just creates a Thread1 object
public static void main(String args[]){
new Thread1("My thread", 2000);
}
}

Comments
~~~~~~~~~~~
public class Thread1 extends Thread
{
//Rowan's comment - Problem with this constructor. Check the
// API for details. Also, you need to capitalise "thread" ie
// Thread myThread, not thread myThread. Plus, you need an
// identifier - eg. myThread (unless you want to say something
// like "new Thread().start();"
protected thread = new Thread("I am the main thread", 10);
//I get an error here
// Main method
public static void main(String[] args)
{
//Rowan's comment - Not sure exactly what you're trying to
// do here, since you've already created your thread above -
// - unless you want to run another thread too??
//Create the thread
//Also, somewhere you need to activate the thread
//eg. myThread.start();
Thread thread = new Thread();
//I get an error here
//Create a loop to print out the thread 5 times
for(int i = 0; i < 5; i++)
//Rowan's comment - need to identify the thread
// eg. System.out.println(Thread.currentThread().getName())
// eg. System.out.println(myThread.getName());
//eg. System.out.println(myThread.toString());
System.out.println(thread);
try
{
//Rowan's comment - You need to specify which thread
//you want to put to sleep.
//If you have a named thread, you could say...
// myThread.sleep(2000);
//If you just want to put the current thread to sleep, you
//could say...
// Thread.currentThread().sleep(2000);
//Put the thread to sleep for 2 seconds
thread.sleep(2000);
}
catch (InterruptedException e)
{
}

//Create a loop to print out the thread 5 more times
for(int j = 0; j < 5; j++)
System.out.println(thread);
}
}
 
Sandra Waller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Rowan, you have no idea how much I appreciate your input. Sandra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic