1.4线程暂停

暂停线程可以用suspend()
恢复线程可以用resume()
但是这两个方法都已经过期作废

正常使用suspend和resume

package com.myThread;

public class Thread1 extends Thread {
    long i = 0;

    public Thread1() {

    }

    public long getI() {
        return i;
    }

    public void setI(long i) {
        this.i = i;
    }

    @Override
    public void run() {
        super.run();
        while (true) {
            i++;
        }
    }
}
package com.test;

import com.myThread.Thread1;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {

        // 测试1
        Thread1 thread1 = new Thread1();
        thread1.start();
        Thread.sleep(500);
        thread1.suspend();//暂停
        System.out.println(System.currentTimeMillis()+":a:"+thread1.getI());
        Thread.sleep(100);
        System.out.println(System.currentTimeMillis()+":a:"+thread1.getI());//观察时间和i的变化
        thread1.resume();//恢复
        Thread.sleep(500);
        thread1.suspend();//暂停
        System.out.println(System.currentTimeMillis()+":b:"+thread1.getI());
        Thread.sleep(100);
        System.out.println(System.currentTimeMillis()+":b:"+thread1.getI());//观察时间和i的变化
        thread1.stop();


    }
}

测试1打印结果

1452594207178:a:276619864
1452594207278:a:276619864
1452594207778:b:513899984
1452594207878:b:513899984

suspend和resume问题1-独占

使用这个组合,使用不好就会照成公共的同步对象独占,无法释放资源,其他线程则获取不到

这里展示一个比较特殊的情况,println()和suspend一起使用

package com.myThread;

public class Thread2 extends Thread {
    long i = 0;


    @Override
    public void run() {
        super.run();
        while (true) {
            i++;
            System.out.println(i);

        }
    }
}
package com.test;

import com.myThread.Thread2;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {

        // 测试1
        Thread2 thread2 = new Thread2();
        thread2.start();
        Thread.sleep(500);
        thread2.suspend();// 暂停
        System.out.println("main end");

    }
}

打印结果

100060
100061
100062
100063
100064
100065

分析:
thread2暂停了可是main没有暂停,所以最后应该输出“main end”
可是却没有输出

先来看println()的源代码

    public void println(String x) {
    synchronized (this) {
        print(x);
        newLine();
    }
    }

synchronized (this)是一个同步锁,而这个锁正在被thread2占用

suspend和resume问题2-不同步

package com.myObject;

public class Object3 {
    private String name = "c";
    private String password = "cc";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    synchronized public void evalute(String name, String password) {
        this.name = name;
        if ("a".equals(Thread.currentThread().getName())) {
            System.out.println("Thread a 暂停");
            Thread.currentThread().suspend();
        }
        this.password = password;
    }

    @Override
    public String toString() {
        return "Object5 [name=" + name + ", password=" + password + "]";
    }

}
package com.test;

import com.myObject.Object3;


public class Test3 {
    public static void main(String[] args) throws InterruptedException {

        // 测试1
        final Object3 object3 = new Object3();
        Thread threada = new Thread(){
            @Override
            public void run() {
                super.run();
                object3.evalute("a", "aa");
            }
        };
        threada.setName("a");
        threada.start();//会在evalute方法中暂停
        Thread.sleep(5000);
        Thread threadb = new Thread(){
            @Override
            public void run() {
                super.run();
                System.out.println(object3.toString());
            }
        };      
        threadb.start();

    }
}

测试1打印结果

Thread a 暂停
Object5 [name=a, password=cc]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值