码迷,mamicode.com
首页 > 编程语言 > 详细

Java 中Thread的sleep、join方法解析

时间:2018-10-24 15:41:34      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:完成   exception   span   ati   addm   public   int   trace   err   

1.Thread中sleep方法作用是使当前线程等待,其他线程开始执行,如果有线程锁,sleep不会让出锁

没有加锁代码如下:

public class SynchronizedSleepMethod {
    public static void main(String[] args) {
        MoneyMethod moneyMethod = new MoneyMethod();
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i);
            t.start();
        }
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i);
            t.start();
        }
    }

}

class MyThread4 implements Runnable {
    MoneyMethod moneyMethod;

    /**
     * 
     */
    public MyThread4(MoneyMethod moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            moneyMethod.addMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class MyThread5 implements Runnable {
    MoneyMethod moneyMethod;

    /**
     * 
     */
    public MyThread5(MoneyMethod moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            moneyMethod.subMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        System.out.println(moneyMethod.money);
        // TODO Auto-generated method stub

    }

}

class MoneyMethod {
    int money = 200;

    public void addMoney() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + ":::run:::" + money);
        money++;
    }

    public void subMoney() throws InterruptedException {

        System.out.println(Thread.currentThread().getName() + ":::run:::" + money);
        money--;
    }

}

结果如下:

t10:::run:::202
t12:::run:::202
t11:::run:::202
t13:::run:::203
t15:::run:::205
t14:::run:::204
t17:::run:::207
t19:::run:::209
t18:::run:::209
t16:::run:::207
t21:::run:::207
t24:::run:::205
t22:::run:::206
t20:::run:::206
t29:::run:::203
t28:::run:::203
t26:::run:::202
t27:::run:::201
t23:::run:::200
t25:::run:::199

加锁代码如下:

public class SynchronizedSleepMethod {
    public static void main(String[] args) {
        MoneyMethod moneyMethod = new MoneyMethod();
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i);
            t.start();
        }
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i);
            t.start();
        }
    }

}

class MyThread4 implements Runnable {
    MoneyMethod moneyMethod;

    /**
     * 
     */
    public MyThread4(MoneyMethod moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            moneyMethod.addMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class MyThread5 implements Runnable {
    MoneyMethod moneyMethod;

    /**
     * 
     */
    public MyThread5(MoneyMethod moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            moneyMethod.subMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        System.out.println(moneyMethod.money);
        // TODO Auto-generated method stub

    }

}

class MoneyMethod {
    int money = 200;

    public synchronized void addMoney() throws InterruptedException {
        money++;
        System.out.println(Thread.currentThread().getName() + ":::run:::" + money);
    }

    public synchronized void subMoney() throws InterruptedException {
        money--;
        System.out.println(Thread.currentThread().getName() + ":::run:::" + money);
    }

}

结果如下:

t10:::run:::201
t18:::run:::202
t17:::run:::203
t12:::run:::204
t13:::run:::205
t14:::run:::206
t15:::run:::207
t16:::run:::208
t11:::run:::209
t19:::run:::210
t22:::run:::209
t21:::run:::208
t20:::run:::207
t25:::run:::206
t24:::run:::205
t23:::run:::204
t26:::run:::203
t27:::run:::202
t29:::run:::201
t28:::run:::200

2.Thread中join()方法阻塞调用此方法的线程(calling thread),直到线程t完成,此线程再继续;

没有join()方法代码如下:

public class JoinMethod {
    public static void main(String[] args) throws InterruptedException {
        MoneyMethod2 moneyMethod = new MoneyMethod2();
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i);
            t.start();
//            t.join(300);
            System.out.println(Thread.currentThread().getName() + ":::run:::1"+i);
        }
        
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i);
            t.start();
//            t.join(300);
            System.out.println(Thread.currentThread().getName() + ":::run:::2"+i);
        }
        System.out.println(Thread.currentThread().getName() + ":::finish:::");
    }
}

class MyThread4 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread4(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.addMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class MyThread5 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread5(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.subMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        System.out.println(moneyMethod.money);
        // TODO Auto-generated method stub

    }

}

class MoneyMethod2 {
    int money = 200;

    public void addMoney() throws InterruptedException {
        money++;
        System.out.println(Thread.currentThread().getName() + ":::add:::" + money);
    }

    public void subMoney() throws InterruptedException {
        money--;
        System.out.println(Thread.currentThread().getName() + ":::sub:::" + money);
    }

}

结果:主线程会提前走掉,然后子线程执行

main:::run:::10
main:::run:::11
main:::run:::12
main:::run:::13
main:::run:::14
main:::run:::20
main:::run:::21
main:::run:::22
main:::run:::23
main:::run:::24
main:::finish:::
t10:::add:::201
t12:::add:::202
t11:::add:::203
t13:::add:::204
t14:::add:::205
t21:::sub:::203
t20:::sub:::203
t24:::sub:::201
t22:::sub:::200
t23:::sub:::201

添加了join后,join的等待时间>线程执行时间,代码如下:

public class JoinMethod {
    public static void main(String[] args) throws InterruptedException {
        MoneyMethod2 moneyMethod = new MoneyMethod2();
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i);
            t.start();
            t.join(3000);
            System.out.println(Thread.currentThread().getName() + ":::run:::1"+i);
        }
        
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i);
            t.start();
            t.join(3000);
            System.out.println(Thread.currentThread().getName() + ":::run:::2"+i);
        }
        System.out.println(Thread.currentThread().getName() + ":::finish:::");
    }
}

class MyThread4 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread4(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.addMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class MyThread5 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread5(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.subMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        System.out.println(moneyMethod.money);
        // TODO Auto-generated method stub

    }

}

class MoneyMethod2 {
    int money = 200;

    public void addMoney() throws InterruptedException {
        money++;
        System.out.println(Thread.currentThread().getName() + ":::add:::" + money);
    }

    public void subMoney() throws InterruptedException {
        money--;
        System.out.println(Thread.currentThread().getName() + ":::sub:::" + money);
    }

}

结果如下:

t10:::add:::201
main:::run:::10
t11:::add:::202
main:::run:::11
t12:::add:::203
main:::run:::12
t13:::add:::204
main:::run:::13
t14:::add:::205
main:::run:::14
t20:::sub:::204
main:::run:::20
t21:::sub:::203
main:::run:::21
t22:::sub:::202
main:::run:::22
t23:::sub:::201
main:::run:::23
t24:::sub:::200
main:::run:::24
main:::finish:::

加入join方法后,线程执行时间>join的等待时间时,代码如下:

public class JoinMethod {
    public static void main(String[] args) throws InterruptedException {
        MoneyMethod2 moneyMethod = new MoneyMethod2();
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread4(moneyMethod), "t1" + i);
            t.start();
            t.join(300);
            System.out.println(Thread.currentThread().getName() + ":::run:::1"+i);
        }
        
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new MyThread5(moneyMethod), "t2" + i);
            t.start();
            t.join(300);
            System.out.println(Thread.currentThread().getName() + ":::run:::2"+i);
        }
        System.out.println(Thread.currentThread().getName() + ":::finish:::");
    }
}

class MyThread4 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread4(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.addMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class MyThread5 implements Runnable {
    MoneyMethod2 moneyMethod;

    /**
     * 
     */
    public MyThread5(MoneyMethod2 moneyMethod) {
        // TODO Auto-generated constructor stub
        this.moneyMethod = moneyMethod;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            moneyMethod.subMoney();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        System.out.println(moneyMethod.money);
        // TODO Auto-generated method stub

    }

}

class MoneyMethod2 {
    int money = 200;

    public void addMoney() throws InterruptedException {
        money++;
        System.out.println(Thread.currentThread().getName() + ":::add:::" + money);
    }

    public void subMoney() throws InterruptedException {
        money--;
        System.out.println(Thread.currentThread().getName() + ":::sub:::" + money);
    }

}

结果如下:

main:::run:::10
main:::run:::11
main:::run:::12
main:::run:::13
main:::run:::14
main:::run:::20
t10:::add:::201
main:::run:::21
t11:::add:::202
main:::run:::22
t12:::add:::203
main:::run:::23
t13:::add:::204
main:::run:::24
main:::finish:::
t14:::add:::205
t20:::sub:::204
t21:::sub:::203
t22:::sub:::202
t23:::sub:::201
t24:::sub:::200

在以上代码中可以发现,在加入join()方法后主线程还是会提前走,但是所有子线程会按照执行顺序执行

Java 中Thread的sleep、join方法解析

标签:完成   exception   span   ati   addm   public   int   trace   err   

原文地址:https://www.cnblogs.com/vstarcui/p/9841769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!