//注意点1. notify唤醒沉睡的线程后,线程会接着上次的执行继续往下执行 class TreadTest implements Runnable{ //由于wait、notify、notifyall在synchronized代码块执行,说明当前线程一定是获取了锁的 @Override public synchronized void run() { while (true){ System.out.println("子线程 开始执行马上等待..."); try{ //线程阻塞,必须先获得锁 //当线程执行wait()方法时候,会释放当前的锁,然后让出CPU,进入等待状态 wait(); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("子线程 执行结束..."); } } } public class ThreadDemo1 { public static void main(String[] args){ //创建对象 TreadTest treadTest = new TreadTest(); //线程初始化 Thread thread = new Thread(treadTest); //启动线程 thread.start(); while (true){ //给对象加锁 synchronized (treadTest){ System.out.println("主线线程 执行开始"); try{ //线程超时 Thread.sleep(100); System.out.println("主线线程 线程睡醒结束主线程启动"); }catch (InterruptedException e){ e.printStackTrace(); } //只唤醒一个等待(对象的)线程并使该线程开始执行 //直到执行完synchronized代码块或是中途遇到wait,然后再次释放锁 //notify和notifyAll的执行只是唤醒沉睡的线程,而不会立即释放锁,锁的释放要看代码块的具体执行情况 //临界区 treadTest.notify(); //唤醒所有等待的线程,哪一个线程被第一个处理取决于操作系统的实现 //treadTest.notifyAll(); } } } }
//创建线程的方式:1. 继承Thread类 2.实现Runnable接口 3.匿名内部类的方式 4.带返回值的线程 5.定时器 //6.线程池的实现 7.lambda表达式实现 public class ThreadDemo2 { public static void main(String[] args){ ThreadLearn threadLearn1 = new ThreadLearn("thread-111"); ThreadLearn threadLearn2 = new ThreadLearn("thread-222"); threadLearn1.start(); threadLearn2.start(); //线程中断没有清除未执行的任务和内存数据 //threadLearn1.stop(); threadLearn1.interrupt(); } } class ThreadLearn extends Thread{ public ThreadLearn(String name){ //调用父类方法改变线程名称 super(name); } @Override public void run() { System.out.println("子线程开始执行 " +getName()); //在终止线程之前完成任务清除工作 while (!interrupted()){ System.out.println("子线程进入循环 " +getName()); try{ Thread.sleep(200); }catch (InterruptedException e){ e.printStackTrace(); } } } }
public class ThreadDemo3 { public static void main(String[] args){ /*new Thread(){ @Override public void run() { System.out.println("线程开始执行了................"); } }.start();*/ /*new Thread(new Runnable() { @Override public void run() { System.out.println("线程开始执行了Runnable................"); } }).start();*/ // 匿名内部类方式实现 // 子类会覆盖父类的方法 多态 JVM的动态分配机制 new Thread(new Runnable() { public void run() { System.out.println("线程1"); } }){ public void run() { System.out.println("线程2"); } }.start(); } }
//任务执行完毕,可以获取结果 public class ThreadDemo4 implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("call 被执行"); Thread.sleep(1000); return 100; } public static void main(String[] args){ ThreadDemo4 threadDemo4 = new ThreadDemo4(); FutureTask<Integer> task = new FutureTask<>(threadDemo4); Thread t = new Thread(task); t.start(); System.out.println("正在执行"); try{ //获取线程执行的结果 Integer res = task.get(); System.out.println(res); }catch (Exception e){ e.printStackTrace(); } } }
public class ThreadDemo5 { public static void main(String[] args){ /*Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("timer task 被执行"); } },0,1000);*/ //创建固定大小的线程池 //ExecutorService executorService = Executors.newFixedThreadPool(10); //根据任务的执行情况创建线程 数量不固定 /*ExecutorService executorService = Executors.newCachedThreadPool(); for(int i=0;i<1000;i++){ executorService.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } executorService.shutdown();*/ List<Integer> list = Arrays.asList(1,2,3,4,5); int res = sum(list); System.out.println(res); } //lambda 表达式 public static int sum(List<Integer> list){ //并行执行计算和 list.parallelStream().forEach(System.out :: println); return list.parallelStream().mapToInt(a -> a).sum(); } }