Java总结篇系列:Java多线程(二)

本文承接上一篇文章《Java总结篇系列:Java多线程(一)》java

四.Java多线程的阻塞状态与线程控制多线程

上文已经提到Java阻塞的几种具体类型。下面分别看下引发Java线程阻塞的主要方法。ide

1.join().net

join —— 让一个线程等待另外一个线程完成才继续执行。如A线程线程执行体中调用B线程的join()方法,则A线程被阻塞,知道B线程执行完为止,A才能得以继续执行。线程

public class ThreadTest {

    public static void main(String[] args) {

        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);

        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                thread.start();
                try {
                    thread.join();    // 当前线程(main线程)须要等待thread线程执行完后才能继续执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class MyRunnable implements Runnable {

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

2.sleep()code

sleep —— 让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。在其睡眠的时间段内,该线程因为不是处于就绪状态,所以不会获得执行的机会。即便此时系统中没有任何其余可执行的线程,出于sleep()中的线程也不会执行。所以sleep()方法经常使用来暂停线程执行。对象

前面有讲到,当调用了新建的线程的start()方法后,线程进入到就绪状态,可能会在接下来的某个时间获取CPU时间片得以执行,若是但愿这个新线程必然性的当即执行,直接调用原来线程的sleep(1)便可。blog

public class ThreadTest {

    public static void main(String[] args) {

        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);

        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 30) {
                thread.start();
                try {
                    Thread.sleep(1);   // 使得thread必然可以立刻得以执行
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class MyRunnable implements Runnable {

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

注:睡一个毫秒级够了,由于CPU不会空闲,会切换到新建的线程。生命周期

 

3.后台线程(Daemon Thread)队列

概念/目的:后台线程主要是为其余线程(相对能够称之为前台线程)提供服务,或“守护线程”。如JVM中的垃圾回收线程。

生命周期:后台线程的生命周期与前台线程生命周期有必定关联。主要体如今:当全部的前台线程都进入死亡状态时,后台线程会自动死亡(其实这个也很好理解,由于后台线程存在的目的在于为前台线程服务的,既然全部的前台线程都死亡了,那它本身还留着有什么用...伟大啊 ! !)。

设置后台线程:调用Thread对象的setDaemon(true)方法能够将指定的线程设置为后台线程。

public class ThreadTest {

    public static void main(String[] args) {
        Thread myThread = new MyThread();
        for (int i = 0; i < 100; i++) {
            System.out.println("main thread i = " + i);
            if (i == 20) {
                myThread.setDaemon(true);
                myThread.start();
            }
        }
    }

}

class MyThread extends Thread {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("i = " + i);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

判断线程是不是后台线程:调用thread对象的isDeamon()方法。

注:main线程默认是前台线程,前台线程建立中建立的子线程默认是前台线程,后台线程中建立的线程默认是后台线程。调用setDeamon(true)方法将前台线程设置为后台线程时,须要在start()方法调用以前。前台线程都死亡后,JVM通知后台线程死亡,但从接收指令到做出响应,须要必定的时间。

 

4.改变线程的优先级/setPriority():

每一个线程在执行时都具备必定的优先级,优先级高的线程具备较多的执行机会。每一个线程默认的优先级都与建立它的线程的优先级相同。main线程默认具备普通优先级。

设置线程优先级:setPriority(int priorityLevel)。参数priorityLevel范围在1-10之间,经常使用的有以下三个静态常量值:

MAX_PRIORITY:10

MIN_PRIORITY:1

NORM_PRIORITY:5

获取线程优先级:getPriority()。

注:具备较高线程优先级的线程对象仅表示此线程具备较多的执行机会,而非优先执行。

public class ThreadTest {

    public static void main(String[] args) {
        Thread myThread = new MyThread();
        for (int i = 0; i < 100; i++) {
            System.out.println("main thread i = " + i);
            if (i == 20) {
                myThread.setPriority(Thread.MAX_PRIORITY);
                myThread.start();
            }
        }
    }

}

class MyThread extends Thread {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("i = " + i);
        }
    }
}

5.线程让步:yield()

上一篇博文中已经讲到了yield()的基本做用,同时,yield()方法还与线程优先级有关,当某个线程调用yiled()方法从运行状态转换到就绪状态后,CPU从就绪状态线程队列中只会选择与该线程优先级相同或优先级更高的线程去执行。

public class ThreadTest {

    public static void main(String[] args) {
        Thread myThread1 = new MyThread1();
        Thread myThread2 = new MyThread2();
        myThread1.setPriority(Thread.MAX_PRIORITY);
        myThread2.setPriority(Thread.MIN_PRIORITY);
        for (int i = 0; i < 100; i++) {
            System.out.println("main thread i = " + i);
            if (i == 20) {
                myThread1.start();
                myThread2.start();
                Thread.yield();
            }
        }
    }

}

class MyThread1 extends Thread {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("myThread 1 --  i = " + i);
        }
    }
}

class MyThread2 extends Thread {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("myThread 2 --  i = " + i);
        }
    }
}
相关文章
相关标签/搜索