JavaSE 基础 第59节 同步问题Java的解决方案
2016-07-02
1 同步问题Java的解决方案
同步方法:当线程进入同步方法的时候,会获得同步方法所属对象的锁,
一旦获得对象锁,则其他线程不能再执行被锁对象的其他任何同步方法。
只有在同步方法执行完毕之后释放了锁,其他线程才能继续执行。
synchronized 方法声明{
}
同步块:
synchronized(资源对象){
//需要进行同步的方法
}
package com.java1995; /** * 打印机类 * * @author Administrator * */ public class Print { /** * * @param name * 学生姓名 * @param eScore * 英语成绩 * @param mScore * 数学成绩 * @param yScore * 语文成绩 */ public void printScore(String name, int eScore, int mScore, int yScore) { System.out.println(name + "的英语成绩:" + eScore); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(name + "的数学成绩:" + mScore); System.out.println(name + "的语文成绩:" + yScore); } }
package com.java1995; /** * 老师类 * @author Administrator * */ public class Teacher implements Runnable{ //封装 private Print p; private String name; private int eScore,mScore,yScore; public Teacher(Print p,String name,int eScore,int mScore,int yScore){ this.p=p; this.name=name; this.eScore=eScore; this.mScore=mScore; this.yScore=yScore; } //封装的实现 public Print getP() { return p; } public void setP(Print p) { this.p = p; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int geteScore() { return eScore; } public void seteScore(int eScore) { this.eScore = eScore; } public int getmScore() { return mScore; } public void setmScore(int mScore) { this.mScore = mScore; } public int getyScore() { return yScore; } public void setyScore(int yScore) { this.yScore = yScore; } //重写run()方法 @Override public void run() { // TODO Auto-generated method stub synchronized (p) { p.printScore(name, eScore, mScore, yScore); } } }
package com.java1995; public class Test { public static void main(String[] args) { Print p = new Print(); Teacher t1 = new Teacher(p, "博尔特", 10, 11, 12); Teacher t2 = new Teacher(p, "加特林", 23, 56, 1); Teacher t3 = new Teacher(p, "苏炳添", 34, 55, 90); Thread th1 = new Thread(t1); Thread th2 = new Thread(t2); Thread th3 = new Thread(t3); th1.start(); th2.start(); th3.start(); } }
package com.java1995; public class Source { synchronized void method1(){ System.out.println("进入method1方法,获得锁"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1执行完毕,释放锁"); } synchronized void method2(){ System.out.println("进入method2方法,获得锁"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method2执行完毕,释放锁"); } }
package com.java1995; public class MyThread extends Thread{ String name; Source s; MyThread(String name,Source s){ this.name=name; this.s=s; } @Override public void run() { // TODO Auto-generated method stub if("method1".equals(name)){ s.method1(); }else{ s.method2(); } } public static void main(String[] args) { Source s=new Source(); MyThread t1=new MyThread("method1",s); MyThread t2=new MyThread("method2",s); t1.start(); t2.start(); } }
【参考资料】