Java并发编程之线程互斥札记
Java并发编程之线程互斥笔记
this是该对象内部,其他地方调用就是new出来的对象。
本质,如果要保证线程的安全,就要做到是对同一个对象加锁。每个线程访问该对象就不会出现线程不同步的问题了。
public class Client { public <span style="color:#ff0000;">static </span>void main(String[] args) { <span style="color:#ff0000;"> MyShow show=new MyShow();//这是在静态方法中(内部类有可以访问外部类的属性的性质)但是里做不到,所以错误。</span> new Thread(new Runnable() { @Override public void run() { show.showInfo("test"); } }).start(); new Thread(new Runnable() { @Override public void run() { } }).start(); } class MyShow{ public void showInfo(String name){ for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
代码修改如下:
public class Client { public static void main(String[] args) { final MyShow show=new MyShow(); new Thread(new Runnable() { @Override public void run() { while(true){ show.showInfo("code"); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ show.showInfo("monkey"); try { Thread.sleep(560); } catch (Exception e) { // TODO: handle exception } } } }).start(); }
//静态内部类等同于外部类 <span style="color:#ff0000;">static</span> class MyShow{ public void showInfo(String name){ for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }注意:内部类可以访问外部类的成员变量,所以要先有外部类,在静态方法中没有。内部类要访问外部类的局部变量,局部变量要加final。静态内部类相当于外部类,可以有静态方法。静态方法和字节码对象关联(字节码已经加载在内存中)
运行结果:
出现错误!!!接下来要解决线程安全的问题。
在临界数据或者是易碎数据加上对象锁关键字如下:
static class MyShow{ public void showInfo(String name){ synchronized (this) { for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } } System.out.println();
this是该对象内部,其他地方调用就是new出来的对象。
或者一个同步方法也可以解决:
static class MyShow{ //当前对象锁 public synchronized void showInfo(String name){ for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } System.out.println(); }
本质,如果要保证线程的安全,就要做到是对同一个对象加锁。每个线程访问该对象就不会出现线程不同步的问题了。
public class Client { public static void main(String[] args) { final MyShow show=new MyShow(); new Thread(new Runnable() { @Override public void run() { while(true){ show.showInfo("code"); show.showInfo2("code"); MyShow.showInfo3("code"); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ show.showInfo("monkey"); show.showInfo2("monkey"); MyShow.showInfo3("monkey"); try { Thread.sleep(560); } catch (Exception e) { // TODO: handle exception } } } }).start(); } static class MyShow{ //当前对象锁 public synchronized void showInfo(String name){ <span style="color:#ff0000;">synchronized(this)</span>{//出问题的地方改成<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color:#ff0000;">MyShow.class即可解决</span>。</span> for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } } System.out.println(); } //当前对象锁 public synchronized void showInfo2(String name){ for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } System.out.println(); } //静态同步方法(静态方法对应字节码) <span style="color:#ff0000;">public static synchronized</span> void showInfo3(String name){ for(int i=0;i<name.length();i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }