赞
踩
暂停线程意味着此线程还可以恢复运行,在java多线程中,可以使用suspend()方法暂停线程,使用resume()方法恢复线程线程运行,但suspend和resume方法都是废除的方法,如果使用不注意,会有一些占用和不同步的问题。
例子:
线程:
- package stopMethod;
-
- public class Thread5 extends Thread{
- private long i=0;
-
- public long getI() {
- return i;
- }
-
- public void setI(long i) {
- this.i = i;
- }
- public void run(){
- while(true){
- i++;
- }
- }
-
- }
运行方法:
- package stopMethod;
-
- public class Run5 {
- public static void main(String[] args) {
-
- try {
- Thread5 t5=new Thread5();
- t5.start();
- Thread.sleep(5000);
- t5.suspend();
- //A组
- System.out.println("A= "+System.currentTimeMillis()+" i= "+t5.getI());
- Thread.sleep(5000);
- System.out.println("A= "+System.currentTimeMillis()+" i= "+t5.getI());
- //B组
- t5.resume();
- Thread.sleep(5000);
- //C组
- System.out.println("B= "+System.currentTimeMillis()+" i= "+t5.getI());
- Thread.sleep(5000);
- System.out.println("B= "+System.currentTimeMillis()+" i= "+t5.getI());
-
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
-
- }
-
- }
结果:
- A= 1474074033705 i= 2500945734
- A= 1474074038706 i= 2500945734
- B= 1474074043706 i= 4999788165
- B= 1474074048706 i= 7513356222
从输出结果可以看出线程可以暂停并可以恢复运行。
如果suspend使用不当,极易造成公共的同步对象的独占,使得其他线程无法访问公共同步对象。
例子:
锁类:
- package suspend_resume;
-
- public class SynchronizedObject {
- 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");
- }
-
- }
main方法
- package suspend_resume;
-
- public class Run {
- public static void main(String[] args) {
-
- try {
- final SynchronizedObject obj=new SynchronizedObject();
- Thread thread1=new Thread(){
- public void run(){
- obj.printString();
- }
- };
- thread1.setName("a");
- thread1.start();
- Thread.sleep(1000);
- Thread thread2=new Thread(){
- public void run(){
- System.out.println("Thread2 启动了,但无法范文printStrin方法");
- obj.printString();
- }
- };
- thread2.start();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- }
结果:
- begin
- a 线程永远suspend了
- Thread2 启动了,但无法范文printStrin方法
另外还有一种情况也会掉进坑里面去
线程:
- package suspend_resume;
-
- public class Thread1 extends Thread {
- private long i=0;
- public void run(){
- while(true){
- i++;
- System.out.println(i);
- }
- }
-
- }
Run方法
- package suspend_resume;
-
- public class Run1 {
- public static void main(String[] args) {
- try {
- Thread1 t1=new Thread1();
- t1.start();
- Thread.sleep(1000);
- t1.suspend();
- System.out.println("main end");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- }
-
- }
结果:
- 127559
- 127560
- 127561
- 127562
- 127563
结果没有输出main end,是因为println源码也有锁,由于suspend一直占用,锁不能被释放
- public void println(String x) {
- synchronized (this) {
- print(x);
- newLine();
- }
- }
在使用suspend方法时也容易出现因为线程的暂停而导致数据不同步的情况。
例子:
测试类:
- package suspend_resume;
-
- public class MyObject {
- private String usename="11";
- private String pwd="22";
-
- public void setValue(String u,String p){
- this.usename=u;
- if("a".equals(u)){
- System.out.println("a线程被暂停!");
- Thread.currentThread().suspend();
- }
- }
- public void printMessage(){
- System.err.println(usename+" "+pwd);
- }
-
- }
Run方法:
- package suspend_resume;
-
- public class SuspendTest {
- public static void main(String[] args) throws InterruptedException {
- final MyObject obj=new MyObject();
- Thread t1=new Thread(){
- public void run(){
- obj.setValue("bb","bbb");
- }
- };
- t1.start();
- Thread.sleep(1000);
- Thread t2=new Thread(){
- public void run(){
- obj.printMessage();
- }
- };
- t2.start();
- }
-
- }
结果:
bb 22
可以看出程序出现值不同步的问题,所以在程序中使用suspend方法一定要注意。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。