Java-线程的一些控制方法

2020-09-01  本文已影响0人  有腹肌的豌豆Z

一、Java 中的线程状态转换

二、线程控制的基本方法

三、isAlive():判定线程是否处于活动状态

//判定线程是否处于活动状态 : --就绪状态 --运行状态 --阻塞状态
public class Demo2 {
    public static void main(String[] args) {
       new ThreadB().start();
    }
}
class ThreadB extends Thread {
    @Override
    public void run() {
        System.out.println("检测线程是否是活动状态");
        System.out.println(Thread.currentThread().isAlive());
    }
}

四、setPriority():设置线程的优先级

//设置线程优先级 MAX_PRIORITY:最大为10 
//   MIN_PRIORITY:最小为1 
//   DEFAULT_PRIORITY:默认为5
public class Demo4 {
    public static void main(String[] args) {
        ThreadD td = new ThreadD();
        ThreadE te = new ThreadE();
        td.start();// 谁先启动,谁抢占资源的概率越大
        te.start();
        td.setPriority(Thread.MAX_PRIORITY);// 设置优先级为最大,10
        te.setPriority(Thread.MIN_PRIORITY);// 设置优先级为最小,1
    }
}
class ThreadD extends Thread {
    @Override
    public void run() {
        // 默认优先级为5
        // System.out.println(Thread.currentThread().getPriority());
        for (int i = 0; i < 10; i++) {
            System.out.println("ThreadD j---" + i);
        }
    }
}
class ThreadE extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("ThreadE i---" + i);
        }
    }
}

ThreadD j---0
ThreadE i---0
ThreadE i---1
ThreadE i---2
ThreadE i---3
ThreadE i---4
ThreadE i---5
ThreadE i---6
ThreadE i---7
ThreadE i---8
ThreadE i---9
ThreadD j---1
ThreadD j---2
ThreadD j---3
ThreadD j---4
ThreadD j---5
ThreadD j---6
ThreadD j---7
ThreadD j---8
ThreadD j---9

五、sleep():将当前线程睡眠指定毫秒数

class TestThread {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        try {                   // 在哪个线程中调用 Sleep,就让哪个线程睡眠
            Thread.sleep(8000); // 主线程睡8秒后,打断子线程
        } catch (InterruptedException e) {
        }
        thread.interrupt();     // 打断子线程
    }
}
class MyThread extends Thread {
    @Override
    public void run() {
        while(true){
            System.out.println("=== "+ new Date()+" ===");
            try {
                sleep(1000); // 每隔一秒打印一次日期
            } catch (InterruptedException e) {
                return;
            }
        }
    }  
}

=== Tue May 09 11:09:43 CST 2017 ===
=== Tue May 09 11:09:44 CST 2017 ===
=== Tue May 09 11:09:45 CST 2017 ===
=== Tue May 09 11:09:46 CST 2017 ===
=== Tue May 09 11:09:47 CST 2017 ===
=== Tue May 09 11:09:48 CST 2017 ===
=== Tue May 09 11:09:49 CST 2017 ===
=== Tue May 09 11:09:50 CST 2017 ===

class TestThread {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        try {                    // 在哪个线程中调用 Sleep,就让哪个线程睡眠
            Thread.sleep(5000); // 主线程睡8秒后,打断子线程
        } catch (InterruptedException e) {
        }
        thread.flag = false; // 打断子线程
    }
}
class MyThread extends Thread {
    boolean flag = true;
    @Override
    public void run() {
        while(flag){
            System.out.println("=== "+ new Date()+" ===");
            try {
                sleep(1000); // 每隔一秒打印一次日期
            } catch (InterruptedException e) {
            }
        }
    }
}

=== Tue May 09 12:21:24 CST 2017 ===
=== Tue May 09 12:21:25 CST 2017 ===
=== Tue May 09 12:21:26 CST 2017 ===
=== Tue May 09 12:21:27 CST 2017 ===
=== Tue May 09 12:21:28 CST 2017 ===

六、join():合并某个线程,相当于方法的调用

class TestThread {
    public static void main(String[] args) {
        MyThread myThread = new MyThread("childThread");
        myThread.start();
        try {
            myThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i = 1; i <= 4; i++){
            System.out.println("I am the mainThread");
        }
    }
}
class MyThread extends Thread {
    public MyThread(String name) {
        super(name);
    }
    @Override
    public void run() {
        for(int i = 1; i <= 4; i++){
            System.out.println("I am " + getName());
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                return;
            }
        }
    }
}

I am childThread
I am childThread
I am childThread
I am childThread
I am the mainThread
I am the mainThread
I am the mainThread
I am the mainThread

在A中调用B的join()函数,等B的函数执行完毕,在执行A的函数。

七、yield():让出 CPU,当前线程进入就绪状态队列等待,给其他线程执行的机会(就让很小的一个时间片段)。

class TestThread {
    public static void main(String[] args) {
        MyThread my1 = new MyThread("t1");
        MyThread my2 = new MyThread("t2");
        my1.start();
        my2.start();
    }
}
class MyThread extends Thread {
    public MyThread(String s) {
        super(s);
    }
    @Override
    public void run() {
        for(int i = 1; i <= 100; i++){
            System.out.println(getName()+":"+i);
            if(i % 10 == 0) {
                Thread.yield(); // 当前线程让出 CPU 一小会儿
            }
        }
    }  
}

八、wait():它会释放掉对象的锁

区别:

1️.当线程调用了 wait() 时,它会释放掉对象的锁。
2️.另一个会导致线程暂停的方法:Thread.sleep(),它会导致线程睡眠指定的毫秒数,但线程在睡眠的过程中是不会释放掉对象的锁的。

九、notify()/notifyAll():唤醒对象的wait pool 中的一个/所有等待线程

十、一个线程变为一个对象的锁的拥有者的三种方法

十一、守护线程,必须在它的start()之前进行设置,通过setDaemon()传入true的参数,当程序只有守护线程时,该程序就可以结束运行了。

//守护线程:在线程启动前设置setDaemon(true)
public class Demo6 {
    public static void main(String[] args) throws Exception {
        ThreadH th = new ThreadH();
        th.setDaemon(true);// 让当前线程设置为守护线程
        th.start();
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            System.out.println("主线程。。。" + i);
        }
    }
}
class ThreadH extends Thread {
    int i;
    @Override
    public void run() {
        while (true) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Hello,---" + ++i);
        }
    }
}

十二、Thread 类中的 start() 和 run() 的区别

上一篇 下一篇

猜你喜欢

热点阅读