java day18 多线程(join、yield、CopyOnWriteArrayList、interrupt、priority)

1、join

线程的合并方法:

  • 运行的线程调用join(),会使当前线程进入等待状态
  • 等待调用join()的线程执行完毕,当前线程才会继续执行
package Day20_Thread3.priority;

/**
 * @Author wzy
 * @Date 0012 2020-12-12 10:52
 * @Version 1.0
 *
 *      线程的优先级
 *              不影响CPU的运行速度,在资源紧缺的情况下,CPU会调用优先级别更高的线程
 *
 */
public class Demo01 {
    public static void main(String[] args) {
        A a = new A();  //实例化A线程的对象
        B b = new B();  //实例化B线程的对象

        a.setPriority(Thread.MAX_PRIORITY);  //将a线程设置为最高级别
        b.setPriority(Thread.MIN_PRIORITY);  //将b线程设置为最低级别

        b.start();  //启动b线程
        a.start();  //启动a线程

        /*
            结论:
                尽管是b线程先启动,但是由于a线程的优先级别更高,故而A线程先启动
         */

    }
   }

class A extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("A线程:"+i);
        }
    }
}

class B extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("B线程:"+i);
        }
    }
}

2、yield

线程的礼让:

  • 当线程调用到yield()会退出运行状态,回到就绪状态,与其他线程一起等待CPU的调度
package Day20_Thread3.yield;

/**
 * @Author wzy
 * @Date 0012 2020-12-12 09:15
 * @Version 1.0
 *      线程的礼让:
 *           当线程调用yield()会退出运行状态,回到就绪状态,与其他线程一起等待CPU的调度
 */
public class Demo01 {
    public static void main(String[] args) {
        new Mythread1().start();
        new Mythread2().start();
    }
}

class Mythread1 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i%2==0){
                Thread.yield();
            }
            System.out.println("Mythread1:"+i);
        }
    }
}

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

3、CopyOnWriteArrayList

  • 线程安全的ArrayList,加强版读写分离
  • 写有锁,读无锁;读写之间不堵塞,优于读写锁
  • 写入时,先copy入一个容器副本,再添加元素,最后替换引用
  • 使用方法与ArrayList无异
package Day20_Thread3.concurrent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author wzy
 * @Date 0012 2020-12-12 10:27
 * @Version 1.0
 *
 *      线程安全的集合
 */
public class Demo01 {
    public static void main(String[] args) {
        //使用ArrayList同时进行读写时,会出现冲突,并报错
//        ArrayList<String> list = new ArrayList<>();

        List<String> list = new CopyOnWriteArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");


        //遍历集合中的数据  一边遍历,一边添加
        for (String str:list){
            list.add("1");
            System.out.println(str);
        }
        System.out.println("------------------");
        for (String str:list){
            System.out.println(str);
        }
    }
}

4、interrupt

线程的中断:

  • 通过interrupt()是线程进入中断状态,但是不会让线程直接中断
  • 当线程处于阻塞状态的时候,要是线程此时进行中断则会出现异常
package Day20_Thread3.interrupt;


/**
 * @Author wzy
 * @Date 0012 2020-12-12 09:32
 * @Version 1.0
 */
public class Demo01 {
    public static void main(String[] args) {
       new Mythread().start();
    }
}


class Mythread extends Thread{
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            //当线程处于中断状态的时候,使用休眠就会抛出InterruptedException错误
            /*try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            if (i==50){
                //中断线程,改变线程的中断状态,改变的是状态,并非是停止线程不运行
                this.interrupt();
//                return;
            }
            System.out.print(i+"   是否中断:");
            System.out.println(this.isInterrupted());   //isInterrupted判断线程是否中断

        }
    }
}
package Day20_Thread3.interrupt;

/**
 * @Author wzy
 * @Date 0012 2020-12-12 09:50
 * @Version 1.0
 *
 *      线程的中断:
 *              通过interrupt()使得线程进入中断状态,但是不能直接中断。
 *              当线程处于阻塞状态时,如果线程进行中断,则会出现异常
 */
public class Demo02 {
    public static void main(String[] args) {
        Bmythread bmythread = new Bmythread();
        bmythread.start();
        new Amythread(bmythread).start();
    }
}


class Amythread extends Thread{
    private Thread thread;
    public Amythread(Thread thread) {
        this.thread = thread;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 200; i++) {
            //当i=5时,设置B线程中断
            if (i==5){
                thread.interrupt();
            }
            System.out.println("A线程:"+i);
        }
    }
}

class Bmythread extends Thread{
    @Override
    public void run() {
        for (int i = 1; i <= 1000; i++) {
            //如果线程处于中断状态,则调用return方法终止线程B
            if (isInterrupted()){
                System.out.println("B线程结束");
                return;
            }
            System.out.println("B线程:"+i);
        }
    }
}

5、priority

线程的优先级,不影响CPU的运行速度且资源紧缺的情况下,CPU会调用优先级别更高的线程

package Day20_Thread3.priority;

/**
 * @Author wzy
 * @Date 0012 2020-12-12 10:52
 * @Version 1.0
 *
 *      线程的优先级
 *              不影响CPU的运行速度,在资源紧缺的情况下,CPU会调用优先级别更高的线程
 *
 */
public class Demo01 {
    public static void main(String[] args) {
        A a = new A();  //实例化A线程的对象
        B b = new B();  //实例化B线程的对象

        a.setPriority(Thread.MAX_PRIORITY);  //将a线程设置为最高级别
        b.setPriority(Thread.MIN_PRIORITY);  //将b线程设置为最低级别

        b.start();  //启动b线程
        a.start();  //启动a线程

        /*
            结论:
                尽管是b线程先启动,但是由于a线程的优先级别更高,故而A线程先启动
         */

    }
   }

class A extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("A线程:"+i);
        }
    }
}

class B extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("B线程:"+i);
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值