线程

我知道的是我一无所知

在网上找图,找到一个合适的、之前存了一份os级别的图但是忘记出处了,抱歉

 

可为是很形象了,一个图还不够的话、再来一张(不可能是同一份)

java中没有running这个状态,对应的java枚举类中也没有,为什么没有呐?之前看到一篇文章说 在jvm中 线程在使用cpu、硬盘事都是出于运行状态且系统级操作很快、状态细分浪费资源,赶脚很有说服力所以写到了这里,文章忘记了……

 

方法:

1、sleep(long millis) 当前线程放弃cpu,有计时参数则计时等待状态无则可能无限等待,不会释放同步锁、同步监听器

      sleep是静态方法,只对当前正在运行状态的线程对象有效

2、join调用join方法的线程对象所在线程需要等另一个线程执行完才执行

      将几个线程合并为单线程执行;通过wait方法实现当main方法调用fun.join时、main获取线程fun对象锁,调用该对象wait等待,直到该对象唤醒main对象

3、wait,notify,notifyAll是object类的方法,需要先获取锁,在synchronized内

      调wait释放锁,当前线程放入等待队列

       notify、notifyAll唤醒,从对应实例的等待队列进入同步队列,同步队列就是我们所说的锁池了,获取时间片的线程接着wait继续执行

4、interrupt是Thread类的方法,sleep、wait、join方法内部会对线程的“中断状态”检查,如中断状态为true,抛出InterruptedException

     interrupted检查并清除当前线程中断状态

https://www.jianshu.com/p/468c660d02da

创建线程

1、继承Thread类,重写run方法

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());
    }
}

class testThread {
 public static void main(String[] args) {
        new MyThread().start();
    }
}

2、实现Runnable接口,重写run方法,thread类包装

public class testThread{
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        new Thread(runnable).start();
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        //something todo 
    }
}

class test{
  void testFun(){
     // 匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());
            }
        }).start();

        // 尾部代码块, 是对匿名内部类形式的语法糖
        new Thread() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());
            }
        }.start();

        // Runnable是函数式接口,所以可以使用Lamda表达式形式
        Runnable runnable = () -> {System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId());};
        new Thread(runnable).start();
  }
}

3、实现callable接口(函数式接口),重写call

public class Main {
    public static void main(String[] args) throws Exception {
    	 // 将Callable包装成FutureTask,FutureTask也是一种Runnable
        MyCallable callable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        new Thread(futureTask).start();

        // get方法会阻塞调用的线程
        Integer sum = futureTask.get();
        System.out.println(Thread.currentThread().getName() + Thread.currentThread().getId() + "=" + sum);
    }
}


class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId() + "\t" + new Date() + " \tstarting...");

        int sum = 0;
        for (int i = 0; i <= 100000; i++) {
            sum += i;
        }
        Thread.sleep(5000);

        System.out.println(Thread.currentThread().getName() + "\t" + Thread.currentThread().getId() + "\t" + new Date() + " \tover...");
        return sum;
    }
}

 

https://www.jianshu.com/p/468c660d02da

https://baijiahao.baidu.com/s?id=1661331426312390277&wfr=spider&for=pc

https://blog.csdn.net/vbirdbest/article/details/81282163?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.channel_param 理论代码很详细

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值