synchronized关键字
方式一1:
package cn.itcast.heima2; public class TraditionalThreadSynchronized { public static void main(String[] args) { // TODO Auto-generated method stub Output outputer = new TraditionalThreadSynchronized().new Output(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output("zhangxiaoxiang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output("lihuoming"); } } }).start(); } class Output { public void output(String name) { int len = name.length(); synchronized (this) { for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); } } } }
非静态synchronize方法:锁对象是this
package cn.itcast.heima2; public class TraditionalThreadSynchronized { public static void main(String[] args) { // TODO Auto-generated method stub Output outputer = new TraditionalThreadSynchronized().new Output(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output("zhangxiaoxiang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output("lihuoming"); } } }).start(); } class Output { public synchronized void output(String name) { int len = name.length(); for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); } } }
静态方法的锁是class对象:
package cn.itcast.heima2; public class TraditionalThreadSynchronized { public static void main(String[] args) { // TODO Auto-generated method stub Output outputer = new TraditionalThreadSynchronized.Output();new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output("zhangxiaoxiang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } outputer.output2("lihuoming"); } } }).start(); } static class Output { public void output(String name) { int len = name.length(); synchronized (Output.class) { for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); } } public static synchronized void output2(String name) { int len = name.length(); for (int i = 0; i < len; i++) { System.out.print(name.charAt(i)); } System.out.println(); } } }