concurrent-1-入门基础

入门基础

线程中断
     //线程中断
    //Thread.currentThread().interrupt() : 中断线程,标志中断
    //Thread.currentThread().isInterrupted  :判断是否有中断标志
    //Thread.interrupted()  :判断是否有中断标志,并去掉中断标志
    public static void main(String[] args) {
        boolean interrupted=Thread.currentThread().isInterrupted();
        System.out.println(interrupted);
        Thread thread = new Thread(
                ()->{
                    int i = 0;
                   while (true) {
                       //判断是否中断
                        if (Thread.currentThread().isInterrupted()) {
                            System.out.println(System.currentTimeMillis() + "   thread is interupted");
                            break;  //终止操作
                        }
                        System.out.println(" is running " + i++);
                    }
                }

        );
        thread.start();
        try {
            thread.sleep(2000);
        } catch (InterruptedException e) {
            thread.interrupt();  //发生异常清除中断标志,再次中断线程
        }
        thread.interrupt();  //线程中断
        System.out.println("                         "  + thread.isInterrupted());
        //输出
         is running 157317
         is running 157318
         1495808115104   thread is interupted
                         true
    }
优先级
    //优先线程
    //对cpu的抢占提高几率
    private static final Object lock=new Object();  // 共有锁,争夺执行权

    public static int count1=0;
    public static int count2=0;

    public static void main(String[] args) {

        Thread lowPriorityThread=new Thread(
                () -> {
                    while (true) {
                        synchronized (lock) {
                            count1++;
                            if (count1 > 1000000) {
                                System.out.println(" lowPriorityThread is end");
                                break;
                            }
                        }
                    }
                }
        );
        Thread hightPriorityThread=new Thread(
                () -> {
                    while (true) {
                        synchronized (lock) {
                            count2++;
                            if (count2 > 1000000) {
                                System.out.println(" hightPriorityThread is end");
                                break;
                            }
                        }
                    }
                }
        );
        lowPriorityThread.setPriority(Thread.MIN_PRIORITY);  // 优先级最低
        hightPriorityThread.setPriority(Thread.MAX_PRIORITY); //优先级最高
        lowPriorityThread.start();
        hightPriorityThread.start();
    }
    //输出
     hightPriorityThread is end
     lowPriorityThread is end
join/yield
    //join :加入线程,目标线程执行完毕后再执行
    //yield :让出当前cpu执行权,下一刻还是有几率能抢到cpu
   public static  volatile  int count = 0 ;

    public static void main(String[] args) {

       Thread thread1 = new Thread(
                ()->{
                    try {
                        System.out.println(System.currentTimeMillis() + " thread1 is sleep ");
                        Thread.currentThread().sleep(3000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    for(count = 0; count < 5; count ++){
                        if (Thread.currentThread().isInterrupted()) {
                            break;
                        }
                    }
                    System.out.println(System.currentTimeMillis() + " thread1 is end");
                }
        ,"thread1");
       thread1.start();
       thread1.yield();  //让出cpu 还是thread1先执行完毕
        System.out.println(count);
        Thread thread2 = new Thread(
                ()->{
                    System.out.println(System.currentTimeMillis() + " thread2 is running");
                    for(count = 5; count < 10; count ++){
                        try {
                            thread1.join();   //等待thread1
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                        if (Thread.currentThread().isInterrupted()) {
                            break;
                        }
                    }
                    System.out.println(System.currentTimeMillis() + "  thread2 is end");
                }
        ,"thread2");
        thread2.start();
        try {
            thread2.join();  //等待thread2
            thread2.yield();  //让出cpu 还是thread2执行完毕后 main执行完毕
        } catch (InterruptedException e) {
        }
        System.out.println(count);
    }
    //输出
    0
1495808271330 thread1 is sleep 
1495808271330 thread2 is running
1495808274330 thread1 is end
1495808274330  thread2 is end
10
等待/唤醒
    //wait :挂起线程,
    //notify: 随机唤醒挂起的线程 
 public static Object lock=new Object();

    public static void main(String[] args) {
        new Thread(
                () -> {
                    synchronized (lock) {
                        System.out.println(System.currentTimeMillis() + " thread1 is start");
                        try {
                            System.out.println(System.currentTimeMillis() + " thread1 is wait ");
                            lock.wait();  //挂起线程
                            System.out.println(System.currentTimeMillis() +"  now thread1 is running again");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(System.currentTimeMillis() + " thread1 is end");
                    }
                }
        ).start();
        new Thread(
                () -> {
                    synchronized (lock) {
                        System.out.println(System.currentTimeMillis() + " thread2 is start");
                        System.out.println(System.currentTimeMillis() + " thread2 is notify thread1 ");
                        lock.notify();  //随机唤醒挂起线程,挂起线程具备执行资格
                        System.out.println(System.currentTimeMillis() + " thread2 is end");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
        ).start();
    }
    //输出
    1495808342892 thread1 is start
    1495808342892 thread1 is wait 
    1495808342892 thread2 is start
    1495808342892 thread2 is notify thread1 
    1495808342892 thread2 is end
守护线程
    //守护线程,主线程执行完毕即停止
    public static void main(String[] args) {
        Thread thread=new Thread(
                ()->{
                    while (true) {
                        System.out.println(" daemon thread is running");
                    }
                }
        );
        thread.setDaemon(true);  //设置守护main线程
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(" main thread is end ");
    }
    //输出
     daemon thread is running
     daemon thread is running
     daemon thread is running
     daemon thread is running
     main thread is end 
线程组
     //线程组,通过thread构造方法传入线程组建立组的关系
 public static void main(String[] args) {
        ThreadGroup threadGroup=new ThreadGroup("group1");
        Thread thread1=new Thread(threadGroup, () -> System.out.println(Thread.currentThread().getThreadGroup().getName() + "-" + Thread.currentThread().getName()), "thread1");
        Thread thread2=new Thread(threadGroup, () -> System.out.println(Thread.currentThread().getThreadGroup().getName() + "-" + Thread.currentThread().getName()), "thread2");
        thread1.start();
        thread2.start();
    }
    //输出
      group1-thread2
      group1-thread1
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值