📄 suspendresume2.java
字号:
class NewThread6 implements Runnable
{
String name;
Thread t;
boolean suspendFlag;
NewThread6(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New thread:"+t);
suspendFlag=false;
t.start();
}
public void run()
{
try
{
for(int i=15;i>0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(200);
synchronized(this)
{
while(suspendFlag)
{
wait();
}
}
}
}
catch(InterruptedException e)
{
System.out.println(name+" interrupted" );
}
System.out.println(name+"exiting");
}
void mysuspend()
{
suspendFlag=true;
}
synchronized void myresume()
{
suspendFlag=false;
notify();
}
}
public class SuspendResume2
{
public static void main(String[] args)
{
NewThread6 ob1=new NewThread6("one");
NewThread6 ob2=new NewThread6("two");
try
{
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending thread one");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Resuming thread one");
ob2.mysuspend();
System.out.println("Suspend thread two");
Thread.sleep(1100);
ob2.myresume();
System.out.println("resuming thread two");
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
try
{
System.out.println("Wainting for thread to finish");
ob1.t.join();
ob2.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -