Reputation: 23
Trying to write a program that will run, suspend, and resume 3 threads.
here's the coding:
class Connectthread implements Runnable
{
public void run()
{
for(int j=0;j<90;j+=10)
System.out.println("Connecting..." + j + " Secs");
}
}
class DLoadthread implements Runnable
{
public void run()
{
for(int d=0;d<60;d+=10)
System.out.println("Downloading..." + d + " Secs");
}
}
class Runthread implements Runnable
{
public void run()
{
for(int r=0;r<120;r+=10)
System.out.println("Running..." + r + " Secs");
}
}
class EHAunitThread
{
Connectthread ct=new Connectthread();
DLoadthread dt=new DLoadthread();
Runthread rt=new Runthread();
public void main(String arg[])
{
//putting threads into ready state.
System.out.print("Starting threads\n");
ct.start();
dt.start();
rt.start();
System.out.print("Sleeping 3 seconds\n");
safeSleep(3000, "Threads first sleep time interrupted\n");
System.out.print("Suspending threads\n");
ct.suspend();
dt.suspend();
rt.suspend();
System.out.print("Sleep 5 seconds\n");
safeSleep(5000, "Threads second sleep time interrupted\n");
System.out.print("Resume threads\n");
ct.resume();
dt.resume();
rt.resume();
try
{
ct.join();
dt.join();
rt.join();
}
catch (InterruptedException e)
{
System.out.print("Join interrupted");
}
System.out.print("Testcase Completed");
System.exit(0);
}
}
it keeps giving me 14 of these error:cannot find symbol
messages when i try to compile it.
To my knowledge, the coding looks correct as far as grammar is concerned. What am I doing wrong here?
Upvotes: 1
Views: 2901
Reputation: 6594
Suspend and resume() are unsafe try this aprocach:
public class Pauser{
private boolean isPaused=false;
public synchronized void pause(){
isPaused=true;
}
public synchronized void resume(){
isPaused=false;
notifyAll();
}
public synchronized void look(){
while(isPaused){
wait();
}
}
}
public class MyRunnable implements Runnable{
Pauser pauser;
public MyRunnable(Pauser pauser){
this.pauser=pauser;
}
public void run(){
while(true){
pauser.look();
}
}
public class Caller{
Pauser pauser=new Pauser();
for(int i = 0; i<1000;i++)
new Thread(new MyRunnable(pauser)).start();
//pause all threads
pauser.pause();
//resume all threads
pauser.resume();
}
Upvotes: 1
Reputation: 62439
Runnable
has no start
method. You need to use the Thread
class to encapsulate the Runnable
s:
Thread ct = new Thread(new Connectthread());
Thread dt = new Thread(new DLoadthread());
Thread rt = new Thread(new Runthread());
Upvotes: 1
Reputation: 328598
Your classes are Runnables and you call Thread methods on them. You need to wrap them in Thread objects:
Thread ct=new Thread (newConnectthread());
Also note that Thread#suspend() is deprecated.
Upvotes: 2