Java pause / suspend thread (suspend()) and resume thread (resume())
•
Java
Suspend() and resume() methods
package ch14; public class MyThread21 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++; } } }
package ch14; public class Test25 { public static void main(String[] args) { try { MyThread21 thread=new MyThread21(); thread.start(); Thread.sleep(5000); //A段 thread.suspend(); System.out.println("A= "+System.currentTimeMillis()+" i= "+thread.getI()); Thread.sleep(5000); System.out.println("A= "+System.currentTimeMillis()+" i= "+thread.getI()); //B段 thread.resume(); Thread.sleep(5000); //C段 thread.suspend(); System.out.println("B= "+System.currentTimeMillis()+" i= "+thread.getI()); Thread.sleep(5000); System.out.println("B= "+System.currentTimeMillis()+" i= "+thread.getI()); } catch(InterruptedException e) { e.printStackTrace(); } } }
A= 1540978346179 i= 2680986095 A= 1540978351179 i= 2680986095 B= 1540978356179 i= 5348657508 B= 1540978361179 i= 5348657508
Exclusive problem
Example 2
package ch14; public class SynchronizedObject1 { synchronized public void printString() { System.out.println("begin"); if (Thread.currentThread().getName().equals("a")) { System.out.println("a线程永远 suspend了!"); Thread.currentThread().suspend(); } System.out.println("end"); } }
package ch14; public class Test26 { public static void main(String[] args) { try { final SynchronizedObject1 object=new SynchronizedObject1(); Thread thread1=new Thread() { @Override public void run() { object.printString(); } }; thread1.setName("a"); thread1.start(); Thread.sleep(1000); Thread thread2=new Thread() { @Override public void run() { System.out.println("thread2启动了,但进入不了printString()方法!所以只会打印1个begin!"); System.out.println("因为printString()方法被a线程锁定并且永远暂停了!"); object.printString(); } }; thread2.start(); } catch(InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
begin a线程永远 suspend了! thread2启动了,但进入不了printString()方法!所以只会打印1个begin! 因为printString()方法被a线程锁定并且永远暂停了!
Example 3
package ch14; public class MyThread22 extends Thread { private long i=0; @Override public void run() { while (true) { i++; } } }
package ch14; public class Test27 { public static void main(String[] args) { try { MyThread22 thread=new MyThread22(); thread.start(); Thread.sleep(1000); thread.suspend(); System.out.println("main end!"); } catch(InterruptedException e) { e.printStackTrace(); } } }
main end!
package ch14; public class MyThread22 extends Thread { private long i=0; @Override public void run() { while(true) { i++; System.out.println(i); } } }
...... 130862 130863 130864 130865 130866 130867
Out of sync problem
Example 4
package ch14; public class MyObject { private String username="1"; private String password="11"; public void setValue(String u,String p) { this.username=u; if(Thread.currentThread().getName().equals("a")) { System.out.println("停止a线程!"); Thread.currentThread().suspend(); } this.password=p; } public void printUsernamePassword() { System.out.println(username+" "+password); } }
package ch14; public class Test28 { public static void main(String[] args) throws InterruptedException { final MyObject myobject=new MyObject(); Thread thread1=new Thread() { public void run() { myobject.setValue("a","aa"); }; }; thread1.setName("a"); thread1.start(); Thread.sleep(500); Thread thread2=new Thread() { public void run() { myobject.printUsernamePassword(); }; }; thread2.start(); } }
停止a线程! a 11
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码