synchronized同步方法
阿新 • • 發佈:2020-10-14
package JavaMultiThread; /* * 例項變數的非執行緒安全 */ public class t3 { public static void main(String[] args) { HasSelfPrivateNum numRef1 = new HasSelfPrivateNum(); HasSelfPrivateNum numRef2 = new HasSelfPrivateNum(); ThreadA thread1 = new ThreadA(numRef1); thread1.start(); ThreadB thread2= new ThreadB(numRef1); thread2.start(); } } class HasSelfPrivateNum{ private int num = 0; synchronized public void addI(String username) { try { if(username.equals("a")) { num = 100; System.out.println("a set over!"); Thread.sleep(2000); }else { num = 200; System.out.println("b set over!"); } System.out.println(username + " num = " + num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } }class ThreadA extends Thread{ private HasSelfPrivateNum numRef; public ThreadA(HasSelfPrivateNum numRef) { super(); this.numRef = numRef; } @Override public void run() { super.run(); numRef.addI("a"); } } class ThreadB extends Thread{ private HasSelfPrivateNum numRef; public ThreadB(HasSelfPrivateNum numRef) { super(); this.numRef = numRef; } @Override public void run() { super.run(); numRef.addI("b"); } }
a set over! a num = 100 b set over! b num = 200
package JavaMultiThread; /* * 多個物件多個鎖 */ public class t3 { public static void main(String[] args) { HasSelfPrivateNum numRef1 = new HasSelfPrivateNum(); HasSelfPrivateNum numRef2 = new HasSelfPrivateNum(); ThreadA thread1 = new ThreadA(numRef1); thread1.start(); ThreadB thread2 = new ThreadB(numRef2); thread2.start(); } } class HasSelfPrivateNum{ private int num = 0; synchronized public void addI(String username) { try { if(username.equals("a")) { num = 100; System.out.println("a set over!"); Thread.sleep(2000); }else { num = 200; System.out.println("b set over!"); } System.out.println(username + " num = " + num); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } class ThreadA extends Thread{ private HasSelfPrivateNum numRef; public ThreadA(HasSelfPrivateNum numRef) { super(); this.numRef = numRef; } @Override public void run() { super.run(); numRef.addI("a"); } } class ThreadB extends Thread{ private HasSelfPrivateNum numRef; public ThreadB(HasSelfPrivateNum numRef) { super(); this.numRef = numRef; } @Override public void run() { super.run(); numRef.addI("b"); } }
a set over! b set over! b num = 200 a num = 100
package JavaMultiThread; /* *synchronized鎖住的是物件(不加鎖) */ public class t1 { public static void main(String[] args) { MyObject object = new MyObject(); ThreadH a = new ThreadH(object); a.setName("A"); ThreadH b = new ThreadH(object); b.setName("B"); a.start(); b.start(); } } class MyObject{ public void methodA() { try { System.out.println("begin methodA threadName = " + Thread.currentThread().getName()); Thread.sleep(5000); System.out.println("end!"); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } class ThreadH extends Thread{ private MyObject object; public ThreadH( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodA(); } }
begin methodA threadName = A begin methodA threadName = B end! end!
package JavaMultiThread; /* *synchronized鎖住的是物件 */ public class t1 { public static void main(String[] args) { MyObject object = new MyObject(); ThreadH a = new ThreadH(object); a.setName("A"); ThreadH b = new ThreadH(object); b.setName("B"); a.start(); b.start(); } } class MyObject{ synchronized public void methodA() { try { System.out.println("begin methodA threadName = " + Thread.currentThread().getName()); Thread.sleep(5000); System.out.println("end!"); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } class ThreadH extends Thread{ private MyObject object; public ThreadH( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodA(); } }
begin methodA threadName = A end! begin methodA threadName = B end!
package JavaMultiThread; public class t1 { public static void main(String[] args) { MyObject object = new MyObject(); ThreadH a = new ThreadH(object); a.setName("A"); ThreadC b = new ThreadC(object); b.setName("B"); a.start(); b.start(); } } class MyObject{ synchronized public void methodA() { try { System.out.println("begin methodA threadName = " + Thread.currentThread().getName()); Thread.sleep(5000); System.out.println("end endTime = " + System.currentTimeMillis()); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } public void methodB() { try { System.out.println("begin methodB threadName = " + Thread.currentThread().getName() + " begin time = " + System.currentTimeMillis()); Thread.sleep(5000); System.out.println("end"); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } class ThreadH extends Thread{ private MyObject object; public ThreadH( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodA(); } } class ThreadC extends Thread{ private MyObject object; public ThreadC( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodB(); } }
begin methodA threadName = A begin methodB threadName = B begin time = 1602684531118 end endTime = 1602684535614 end
package JavaMultiThread; public class t1 { public static void main(String[] args) throws InterruptedException { MyObject object = new MyObject(); ThreadH a = new ThreadH(object); a.setName("A"); ThreadC b = new ThreadC(object); b.setName("B"); a.start(); Thread.sleep(500); b.start(); } } class MyObject{ synchronized public void methodA() { try { System.out.println("begin methodA threadName = " + Thread.currentThread().getName()); Thread.sleep(5000); System.out.println("end endTime = " + System.currentTimeMillis()); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } synchronized public void methodB() { try { System.out.println("begin methodB threadName = " + Thread.currentThread().getName() + " begin time = " + System.currentTimeMillis()); Thread.sleep(5000); System.out.println("end"); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } class ThreadH extends Thread{ private MyObject object; public ThreadH( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodA(); } } class ThreadC extends Thread{ private MyObject object; public ThreadC( MyObject object) { //用MyObject物件建立Threada程序 super(); this.object = object; } @Override public void run() { super.run(); object.methodB(); } }
begin methodA threadName = A end endTime = 1602684573421 begin methodB threadName = B begin time = 1602684573421 end