package cn.mutipart.thd; public class Test02 { public static void main(String[] args) { MyPainter p = new MyPainter(); new Thread(){ public void run() { while(true){ try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } p.print1(); } }; }.start(); new Thread(){ public void run() { while(true){ try { Thread.sleep(5); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } p.print2(); } }; }.start(); } } class MyPainter { private String lock = new String("线程锁"); public void print1(){ synchronized (lock) { System.out.print("[不"); System.out.print("是"); System.out.print("广"); System.out.print("告]"); System.out.println(); } } public void print2(){ synchronized (lock) { System.out.print("[X"); System.out.print("X"); System.out.print("X"); System.out.print("X]"); System.out.println(); } } }