初始化—就緒—運行—終止spring
Sleep : 超時等待,過了一段時間就會進入就緒狀態進行競爭cpu資源。多線程
Wait: 等待狀態,沒有經過notify 或者 notifyAll 喚醒,就會一直進行等待。異步
Block: block io 或者 遇到加鎖的代碼時, 接受到數據或者獲取到鎖就會到運行狀態,也有可能直接進入dead 狀態。ide
public class NewThread implements Runnable { @Override public synchronized void run() { while (true) { System.out.println("線程運行了..."); try { // Thread.sleep(100); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { //建立線程並執行線程任務 NewThread target = new NewThread(); Thread thread = new Thread(target); //線程啓動 thread.start(); while (true) { synchronized (target) { System.out.println("主線程"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } target.notify(); } } } }
建立線程:線程
public class Demo1 extends Thread { public Demo1(String name) { super(name); } @Override public void run() { while (true) { System.out.println(getName() + "線程執行..."); } } public static void main(String[] args) { Demo1 d1 = new Demo1("first-thread"); Demo1 d2 = new Demo1("second-thread"); //(守護線程) 即便線程沒有執行完畢,只要主線程執行完了,線程就會退出 d1.setDaemon(true); d2.setDaemon(true); d1.start(); d2.start(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
線程的中斷:code
使用stop()方式沒有釋放鎖和資源,只是讓線程無限期的等待下去
推薦使用interrupt()繼承
public class Demo1 extends Thread { public Demo1(String name) { super(name); } @Override public void run() { while (!interrupted()) { System.out.println(getName() + "線程執行..."); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Demo1 d1 = new Demo1("first-thread"); Demo1 d2 = new Demo1("second-thread"); d1.start(); d2.start(); d1.interrupt(); } }
/** * 做爲線程任務存在 */ public class Demo2 implements Runnable { @Override public void run() { while (true) { System.out.println("thread running"); } } public static void main(String[] args) { Thread thread = new Thread(new Demo2()); thread.start(); } }
public class Demo3 { public static void main(String[] args) { //該線程僅建立一次 new Thread() { @Override public void run() { while (true) { System.out.println("thread start..."); } } }.start(); new Thread(new Runnable() { @Override public void run() { System.out.println("thread start..."); } }).start(); //執行結果爲sub new Thread(new Runnable() { @Override public void run() { System.out.println("runnable"); } }) { @Override public void run() { System.out.println("sub"); } }.start(); } }
public class Demo4 implements Callable<Integer> { public static void main(String[] args) throws ExecutionException, InterruptedException { //執行的任務 Demo4 d = new Demo4(); FutureTask<Integer> task = new FutureTask<Integer>(d); Thread t = new Thread(task); t.start(); System.out.println("我先乾點別的"); //拿回返回結果 Integer integer = task.get(); System.out.println("線程執行的結果爲:" + integer); } @Override public Integer call() throws Exception { System.out.println("正在進行緊張的計算"); Thread.sleep(3000); return 1; } }
public class Demo5 { public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { //實現定時任務 System.out.println("timertask is run"); } }, 0, 1000); } }
newFixedThreadPool:接口
public class Demo6 { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(10); for (int i = 0; i < 100; i++) { threadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } threadPool.shutdown(); } }
newCachedThreadPool:資源
public class Demo6 { public static void main(String[] args) { //比較智能的線程池,不夠用就建立,夠用就回收 ExecutorService threadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { threadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } threadPool.shutdown(); } }
public class Demo7 { public int add(List<Integer> values) { // values.stream().forEach(System.out::println); return values.parallelStream().mapToInt(a -> a).sum(); } public static void main(String[] args) { List<Integer> values = Arrays.asList(10, 20, 30, 40); int result = new Demo7().add(values); System.out.println(result); } }
Main方法:get
public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); DemoService bean = ac.getBean(DemoService.class); bean.a(); bean.b(); } }
相關配置
@Configuration @ComponentScan("com.autohome.data.realtime.demo") @EnableAsync public class Config { }
異步代碼:
@Service public class DemoService { @Async public void a() { while (true) { System.out.println("a"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } @Async public void b() { while (true) { System.out.println("b"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }