00并发编程_线程基础和线程之间的共享和协作

CPU核心数和线程的关系

核心数:线程数 = 1:1   超线程技术-> 1:2

CPU时间片轮转机制

RR调度 上下文切换

什么是进程和线程

进程:程序运行资源分配的最小单位,进程内部有多少个线程,会共享这个进程的资源

线程:CPU调度的最小单位

并行和并发

并发:与时间单位,在单位时间内处理事情的能力

并行:同一时刻可以处理事情的能力

Java里的程序天生就是多线程的,有几种新启线程的方式

①继承Thread 

public class UseThread {

    private int ticket = 10;

    public void saleTicket(){
       while (ticket > 0){
               this.ticket--;
               System.out.println(this.ticket);
            }
        }
     public static void main(String[] args){
        UseThread ut = new UseThread();
        for (int i = 0; i < 2; i++) {
            new Thread(()->{
                ut.saleTicket();
            }).start();
        }
        
    }
}

②接口Runable

实现Runnable接口,无返回值


public class UseRun implements Runnable{

        @Override
        public void run() {
            System.out.println("I am implements Runnable");
        }
    

     public static void main(String[] args) {

         UseRun useRun = new UseRun();
         Thread t1 = new Thread(useRun);
         t1.start();
      }
}

③ 接口Callable

    实现Callable接口,允许有返回值

public class UseCall implements Callable<String>{

        @Override
        public String call() throws Exception {
            System.out.println("I am implements Callable");
            return "Callable";
        }

    public static void main(String[] args) throws ExecutionException, InterruptedException{
        UseCall useCall = new UseCall();
        FutureTask<String> futureTask = new FutureTask<>(useCall);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

    }
}

有开始就有结束,怎么样才能让Java的线程安全停止工作呢?

  Stop()还是Interrupt()、isinterrupted()、static方法interrupted(),深入理解这些方法

  1. 自然执行完,2抛出异常

stop(), resume(),suspend()线程不会释放资源

线程是协作式

Interrupt() 中断一个线程,并不是强行关闭这个线程,打个招呼,中断标志位置为true,

isinterrupted()判定当前线程是否处于中断状态

static方法interrupted() 判定当前线程是否处于中断状态,中断标志位改为false

public class EndThread {

    private static class UseThread extends Thread{

        public UseThread(String name){
            super(name);
        }

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            while (!isInterrupted()){
                System.out.println(threadName + "is running");
            }
            System.out.println(threadName+ "interrupted flag is" + isInterrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread endThread = new Thread("endThread");
        endThread.start();
        Thread.sleep(20);
        endThread.interrupt();
    }
}
private static class UseRunnable implements Runnable{

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            while (Thread.currentThread().isInterrupted()){
                System.out.println(threadName + "is run");
            }
            System.out.println(threadName + "interrupted is"
                    +Thread.currentThread().isInterrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        UseRunnable useRunnable = new UseRunnable();
        Thread  endThread = new Thread(useRunnable,"endThread");
        endThread.start();
        Thread.sleep(20);
        endThread.interrupt();
    }
}

某个方法会抛出InterruptedException,线程的中断标志位会被复位成false,需要我们自己在catch里再次中断

public class HasInterruptedException {

    public static class UseThread extends Thread{

        public UseThread(String name){
            super(name);
        }

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            while(!isInterrupted()){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(threadName + "interrupted flag is "
                    + isInterrupted());
                    interrupt();
                    e.printStackTrace();
                }
                System.out.println("while " + threadName);
            }
            System.out.println("run" +threadName);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        UseThread useThread = new UseThread("HasInterruptedException");
        useThread.start();
        Thread.sleep(30);
        useThread.interrupt();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值