一、线程声明及线程间通信
java">package org.example;import java.util.Random;public class ThreadTest {public void test1() {Thread t1 = new Thread(()->{System.out.println("t1...");});Thread t2 = new Thread(()->{System.out.println("t2...");});// new 线程初始化System.out.println("t1 new:" + t1.getState());t1.start();t2.start();// start 线程执行System.out.println("t1 start:" + t1.getState());}public void threadBlockTest() {Object obj = new Object();Thread t1 = new Thread(()->{synchronized (obj) {while (true) {System.out.println("t1 lock run ..");try {Thread.sleep(new Random().nextInt(1000));} catch (InterruptedException e) {throw new RuntimeException(e);}}}});Thread t2 = new Thread(()->{synchronized (obj) {System.out.println("t2 lock run ..");}});t1.start();t2.start();System.out.println("t1 status:" + t1.getState());System.out.println("t2 status:" + t2.getState());}public void threadWaitingTest() {Object object = new Object();Thread t1 = new Thread(()->{synchronized (object) {try {System.out.println("t1 start to wait...");object.wait();System.out.println("t1 wait end");} catch (InterruptedException e) {throw new RuntimeException(e);}}});Thread t3 = new Thread(()->{synchronized (object) {try {System.out.println("t3 start to wait...");object.wait();System.out.println("t3 wait end");} catch (InterruptedException e) {throw new RuntimeException(e);}}});Thread t2 = new Thread(()->{synchronized (object) {System.out.println("t2 start ...");int i = 0;while (i<5) {System.out.println("t2 执行次数:" + i);i++;}object.notify();System.out.println("t2 notifyAll");}});t1.start();t2.start();t3.start();System.out.println("t1 state:" + t1.getState());System.out.println("t1 state:" + t1.getState());System.out.println("t2 state:" + t2.getState());System.out.println("t3 state:" + t3.getState());System.out.println("t3 state:" + t3.getState());}public void threadTimeWaitingTest() throws InterruptedException {Thread t1 = new Thread(()->{try {System.out.println("t1 sleep 1000 start");Thread.sleep(1000);System.out.println("t1 sleep 1000 end");} catch (InterruptedException e) {throw new RuntimeException(e);}});t1.start();t1.join();System.out.println("t1 state:" +t1.getState());}public void joinTest() {Thread t3 = new Thread(()->{try {System.out.println("t3 start...");Thread.sleep(1000);System.out.println("t3 end.");} catch (InterruptedException e) {throw new RuntimeException(e);}});Thread t2 = new Thread(()->{try {System.out.println("t2 start...");t3.join();System.out.println("t2 end.");} catch (InterruptedException e) {throw new RuntimeException(e);}});Thread t1 = new Thread(()->{try {System.out.println("t1 start...");t2.join();System.out.println("t1 end.");} catch (InterruptedException e) {throw new RuntimeException(e);}});t1.start();t2.start();t3.start();}public static void main(String[] args) throws InterruptedException {ThreadTest threadTest = new ThreadTest();
// threadTest.test1();// threadTest.threadBlockTest();threadTest.threadWaitingTest();// threadTest.threadTimeWaitingTest();// threadTest.joinTest();}
}
二、线程间执行结果接收处理
java">package org.example;import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;public class MyCallbale implements Callable<Integer> {@Overridepublic Integer call() throws Exception {int sum = 0;for (int i = 0; i < 10; i++) {sum += i;}return sum;}
}class FutureTeskExample {public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallbale myCallbale = new MyCallbale();FutureTask<Integer> integerFutureTask = new FutureTask<>(myCallbale);Thread thread = new Thread(integerFutureTask);thread.start();Integer i = integerFutureTask.get();System.out.println("Result :" + i);}
}
三、线程池
java">package org.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;public class ExecutorServiceExample {public void multipleCallableExample() throws InterruptedException, ExecutionException {ExecutorService executorService = Executors.newFixedThreadPool(3);List<Callable<Integer>> callables = new ArrayList<>();callables.add(()->1);callables.add(()->2);callables.add(()->3);List<Future<Integer>> futures = executorService.invokeAll(callables);for (Future<Integer> future : futures) {Integer i = future.get();System.out.println(i);}}public void singalThreadPool () throws ExecutionException, InterruptedException {ExecutorService executorService = Executors.newFixedThreadPool(1);MyCallbale myCallbale = new MyCallbale();Future<Integer> future = executorService.submit(myCallbale);Integer i = future.get();System.out.println("result :" + i);}public static void main(String[] args) throws ExecutionException, InterruptedException {ExecutorServiceExample executorServiceExample = new ExecutorServiceExample();executorServiceExample.singalThreadPool();ExecutorServiceExample executorServiceExample1 = new ExecutorServiceExample();executorServiceExample1.multipleCallableExample();}
}