Using a modification of the synchronized keyword method in all of the code are synchronized
/ *
Lock * non-static function is synchronous: the this
lock * static synchronization function is: bytecode object .class file
* /
public class demon_syn2 { public static void main (String [] args) { Final Printer2 new new Printer2 P = (); // anonymous inner classes in the use of its local variables in the process, which must be modified with a variable Final new new the Thread () { public void RUN () { the while ( to true ) { p.print1 (); } } } .start (); new new the Thread () { public void RUN () { the while ( to true ) { p.print2 (); } } }.start(); } } class printer2{ demo1 d2 = new demo1(); public synchronized void print1(){ System.out.print("黑"); System.out.print("马"); System.out.print("程"); System.out.print("序"); System.out.print("员"); System.out.print("\r\n"); } public synchronized void print2(){ System.out.Print ( "Biography"); System.out.print("智"); System.out.print("播"); System.out.print("客"); System.out.print("\r\n"); } } class demo1{ }