package com.thread;/** * 線程同步 ,子線程執行一次,主線程後在執行一次 * 大家輪流執行 * * @author sky * */public class TraditionThread {public static void main(String[] args) {new TraditionThread().init();}public void init (){final Business business = new Business();// 直接重載父類的方法 子線程new Thread(){@Overridepublic void run() {for (int i = 1; i <=3; i++) {business.sub();}}}.start();// 主線程for (int i = 1; i <=3; i++) {business.main();}}class Business {private boolean isShouldSub = true; // true:子線程開始執行, false:主線程開始執行public synchronized void sub (){if (!isShouldSub){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} for (int i = 1; i <=2; i++) { System.out.println("print sub ");}isShouldSub = false;this.notify(); }public synchronized void main (){ // true 等待 if (isShouldSub){ try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} }for (int i = 1; i <= 3; i++) {System.out.println("main");}// 執行完了之後,通知子線程執行isShouldSub = true;this.notify();}}}