Suspend和Resume:
Suspend和Resume使用方法:
以下例子证明了线程确实被暂停了,而且还可以恢复成运行状态。
public class SuspendResumeThread extends Thread{ private long i = 0; public long getI() { return i; } public void setI(long i) { this.i = i; } @Override public void run() { while(true){ i++; } } } public class ThreadRunMain { public static void main(String[] args) { testSuspendResumeThread(); } public static void testSuspendResumeThread(){ try { SuspendResumeThread srt = new SuspendResumeThread(); srt.start(); Thread.sleep(5000); srt.suspend(); System.out.println("A= " + System.currentTimeMillis() + " i=" + srt.getI()); Thread.sleep(5000); System.out.println("A= " + System.currentTimeMillis() + " i=" + srt.getI()); srt.resume(); Thread.sleep(5000); srt.suspend(); System.out.println("A= " + System.currentTimeMillis() + " i=" + srt.getI()); Thread.sleep(5000); System.out.println("A= " + System.currentTimeMillis() + " i=" + srt.getI()); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果:
Suspend和Resume缺点1-独占:
public class SuspendResumeObject { synchronized public void printString(){ System.out.println("begin"); if(Thread.currentThread().getName().equals("a")){ System.out.println("a thread will suspend."); Thread.currentThread().suspend(); } System.out.println("SuspendResumeObject end"); } } public class ThreadRunMain { public static void main(String[] args) { testSuspendResumeObject(); } public static void testSuspendResumeObject(){ try { final SuspendResumeObject object = new SuspendResumeObject(); Thread t = new Thread(){ @Override public void run() { object.printString(); } }; t.setName("a"); t.start(); Thread.sleep(1000); Thread t2 = new Thread(){ @Override public void run() { System.out.println("Thread 2 start, but can't enter printString()"); System.out.println("Because of printString() is blocked by thread a"); object.printString(); System.out.println("Thread 2 complete"); } }; t2.start(); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果:
public class SuspendResumePrintStreamIssue extends Thread{ private long i = 0; @Override public void run() { while (true){ // try { i++; // Thread.sleep(1); System.out.println(i); // } catch (InterruptedException e) { // e.printStackTrace(); // } } } } public class ThreadRunMain { public static void main(String[] args) { testSuspendResumePrintStreamIssue(); } public static void testSuspendResumePrintStreamIssue(){ try { SuspendResumePrintStreamIssue srpsi = new SuspendResumePrintStreamIssue(); srpsi.start(); Thread.sleep(1000); srpsi.suspend(); System.out.println("Main end."); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果:
同样是独占,当程序运行到PrintStream对象println()方法内部停止时,同步锁未被释放。这导致main()方法中的System.out.println("Main end.");不能被执行。
Suspend和Resume缺点2-数据不一致:
public class SuspendResumeDataIssue { private String userName = "1"; private String password = "11"; synchronized public void setValue(String u, String p){ this.userName = u; if(Thread.currentThread().getName().equals("a")){ System.out.println("Thread a suspend."); Thread.currentThread().suspend(); } this.password = p; } public void printUserNamePassword(){ System.out.println(userName + " " + password); } } public class ThreadRunMain { public static void main(String[] args) { testSuspendResumeDataIssue(); } public static void testSuspendResumeDataIssue(){ try { SuspendResumeDataIssue srdi = new SuspendResumeDataIssue(); Thread t1 = new Thread(){ @Override public void run() { srdi.setValue("a", "aa"); } }; t1.setName("a"); t1.start(); Thread.sleep(500); Thread t2 = new Thread(){ @Override public void run() { srdi.printUserNamePassword(); } }; t2.start(); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果: