05月19, 2017

Java多线程其他

1.interrupt()方法

  interrupt方法不会真正中断线程,它只会清楚线程的wait,sleep,join的受阻状态,时线程重新获得CPU的执行权。

  此时如果再次调用线程的wait,sleep,join方法,将会抛出一个InterruptedException异常


package threadinterrupt;

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

        Thread t = new Thread(new Runnable() {
            private boolean flag = true;
            @Override
            public void run() {
                synchronized (this) {
                    int i = 1;
                    while(flag){

                        System.out.println("=========" + i++ + "=========");
                        if (i > 10) {
                            try {
                                System.out.println("子线程将停止执行");
                                i =0;
                                wait();
                            } catch (InterruptedException e) {
                                System.out.println("第二次调用wait()时抛出异常");
                                e.printStackTrace();
                                break;
                            }
                        }
                    }
                }
            }
        });

        t.start();
        Thread.sleep(2000);
        t.interrupt();
        System.out.println("子线程将继续执行");
    }
}

  

2.join()方法,t1.join方法代表着当前线程放弃CPU执行资格,需要等到t1执行完毕时才能获得CPU执行资格

package threadinterrupt;

import java.lang.Thread.State;

public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        Runnable r = new  Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName()+":========"+i+"========");
                }
            }
        };

        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start();

//        System.out.println("主线程需要等到t1执行完毕才会执行");
//        t1.join();

        if(t1.getState() == State.RUNNABLE){
            System.out.println("【没有】t1.join()时这句话会被执行");
        }

        if(t1.getState() == State.TERMINATED){
            System.out.println("【有】t1.join()时这句话会被执行");
        }
    }
}

  

3.yield()方法,暂停当前正在执行的线程对象,并执行其他线程,该方法不会让该线程放弃CPU的执行权,该线程任然可以争夺CPU执行权

package threadinterrupt;

public class ThreadYield {
    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName()
                            + ":========" + i + "========");
                    Thread.yield();
                }
            }
        };

        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);

        System.out.println("在子线程的run方法中添加Thread.yield();两个线程将会交替(不是绝对交替)");
        t1.start();
        t2.start();

    }
}

  

4.setDaemon(boolean on)方法,将该线程标记为守护线程或用户线程。

5.setPriority(int newPriority) 更改线程的优先级(1-10)数字越大,优先级越高

本文链接:https://www.qiangshuidiyu.xin/post/2017-05-19-01.html

-- EOF --

Comments