suspendresume.java

来自「java 数据结构的一些简单实例和一些线程的应用主要是面向初学者」· Java 代码 · 共 70 行

JAVA
70
字号
class NewThread5 implements Runnable
{
	String name;
	Thread t;
	
	 NewThread5( String threadname)
	{
		name=threadname;
		t=new Thread(this,name);
		System.out.println("New thread :"+t);
		t.start();
	}
	public void run()
	{
		try
		{
			for(int i=15;i>0;i--)
			{
				System.out.println(name+":"+i);
				Thread.sleep(200);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println(name+"interrupted.");
		}
		System.out.println(name+" "+"exiting .");
	}
}

public class SuspendResume 
{
    public static void main(String[] args)
    {
    	NewThread5 ob1=new NewThread5("one");
    	NewThread5 ob2=new NewThread5("two");
    	
    	try
    	{
    		Thread.sleep(1000);
    		ob1.t.suspend();
    		System.out.println("suspending thread one");
    		Thread.sleep(1000);
    		ob1.t.resume();
    		System.out.println("resuming thread one");
    		ob2.t.suspend();
    		System.out.println("suspending thread two");
    		Thread.sleep(1000);
    		ob2.t.resume();
    		System.out.println("resume thread two");
    	}
    	catch(InterruptedException e)
    	{
    		System.out.println("Main thread Interrupted");
    	}
    	
    	try
    	{
    		System.out.println("Wainting for threads 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 + =
减小字号Ctrl + -
显示快捷键?