概念:进程数:运行中的应用,一个进程中至少有一条线程
线程数:CPU进行任务调度的基本单位。(时间片)
线程的态:
新生:new born
就绪:ready thread.start()
运行:running
阻塞:sleep(int millisecs);
wait(int millisecs); //millisecs可省
nootify()//唤醒一个线程
notifyAll()//Object notifyAll() 方法用于唤醒在该对象上等待的所有线程。
死亡:dead();
句柄数:交互类组件在当前系统下的身份识别码。
语法:import java.util.concurrent.*线程安全导包
Thread
Runnable task = () -> { }; Thread thread = new Thread(task,"1"); Thread thread1=new Thread(task,"2");
大型任务:
class MyThread extends Thread{...}
Class MyRuannable implement Runnable{...}
线程方法t.start()//启动线程
t.join()//强制等待
Thread.sleep(int millisecs)//休眠
//syschronized(Object lock){ if(条件){
lock.wait;
}else{
lock,notify();//只有两条线程
lock.notifyAll();//两条以上
}
}
//同步方法:锁对象为当前对象(this)//静态同步方法:锁对象为当前类型(A.class)
线程池://小任务 线程的数量不设上限 ExecutorService poolOne= Executors.newCachedThreadPool(); poolOne.submit(()->{}); FuturesubmitOne= poolOne.submit(()->{ return 0L; }); //大任务 固定数量线程** ExecutorService poolTwo=Executors.newFixedThreadPool(5); poolTwo.submit(()->{}); Future submitTwo= poolTwo.submit(()->{ return 0L; }); //单一任务,唯一任务 ExecutorService poolThree= Executors.newSingleThreadExecutor(); poolThree.submit(()->{}); Future submitSingle=poolThree.submit(()->{ return 0L; }); //调度任务 定期任务 线程数量固定 ScheduledExecutorService poolFour= Executors.newScheduledThreadPool(5); //每两次任务开始之间固定,适用于每两次任务之间无依赖关系 poolFour.scheduleAtFixedRate(()->{},5,10,TimeUnit.SECONDS); //每两次任务(从上次任务结束至下次任务开始之间固定间隔);适用于任何两次任务之间有依赖关系 poolFour.scheduleWithFixedDelay(()->{},5,10,TimeUnit.SECONDS); } pool.shutdown();//关闭线程池



