1.java多線程加鎖是對誰加鎖?
答:當然是對共享資源加鎖啊,對誰進行訪問修改,就對象進行加鎖。以便使多線程按序訪問此共享對象
比如:
在具體的Java代碼中需要完成一下兩個操作:
把競爭訪問的資源類Foo變量x標識為private;
同步哪些修改變量的代碼,使用synchronized關鍵字同步方法或代碼。
package cn.thread; public class Foo2 { private int x = 100; public int getX() { return x; } //同步方法 public synchronized int fix(int y) { x = x - y; System.out.println("線程"+Thread.currentThread().getName() + "運行結束,減少“" + y + "”,當前值為:" + x); return x; } // //同步代碼塊 // public int fix(int y) { // synchronized (this) { // x = x - y; // System.out.println("線程"+Thread.currentThread().getName() + "運行結束,減少“" + y // + "”,當前值為:" + x); // } // // return x; // } }
package cn.thread; public class MyRunnable2 { public static void main(String[] args) { MyRunnable2 run = new MyRunnable2(); Foo2 foo2=new Foo2(); MyThread t1 = run.new MyThread("線程A", foo2, 10); MyThread t2 = run.new MyThread("線程B", foo2, -2); MyThread t3 = run.new MyThread("線程C", foo2, -3); MyThread t4 = run.new MyThread("線程D", foo2, 5); t1.start(); t2.start(); t3.start(); t4.start(); } class MyThread extends Thread { private Foo2 foo2; /**當前值*/ private int y = 0; MyThread(String name, Foo2 foo2, int y) { super(name); this.foo2 = foo2; this.y = y; } public void run() { foo2.fix(y); } } }
線程線程A運行結束,減少“10”,當前值為:90 線程線程C運行結束,減少“-3”,當前值為:93 線程線程B運行結束,減少“-2”,當前值為:95 線程線程D運行結束,減少“5”,當前值為:90
比如:
對於同步,除了同步方法外,還可以使用同步代碼塊,有時候同步代碼塊會帶來比同步方法更好的效果。
追其同步的根本的目的,是控制競爭資源的正確的訪問,因此只要在訪問競爭資源的時候保證同一時刻只能一個線程訪問即可,因此Java引入了同步代碼快的策略,以提高性能。
在上個例子的基礎上,對oper方法做了改動,由同步方法改為同步代碼塊模式,程序的執行邏輯並沒有問題。
package cn.thread; /** * 線程同步方法 * * @author 林計欽 * @version 1.0 2013-7-24 上午10:12:47 */ public class ThreadSynchronizedCode { public static void main(String[] args) { ThreadSynchronizedCode t = new ThreadSynchronizedCode(); User u = t.new User("張三", 100); MyThread t1 = t.new MyThread("線程A", u, 20); MyThread t2 = t.new MyThread("線程B", u, -60); MyThread t3 = t.new MyThread("線程C", u, -80); MyThread t4 = t.new MyThread("線程D", u, -30); MyThread t5 = t.new MyThread("線程E", u, 32); MyThread t6 = t.new MyThread("線程F", u, 21); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); } class MyThread extends Thread { private User u; /**存款金額*/ private int y = 0; MyThread(String name, User u, int y) { super(name); this.u = u; this.y = y; } public void run() { u.oper(y); } } class User { /** 賬號 */ private String code; /** 余額 */ private int cash; User(String code, int cash) { this.code = code; this.cash = cash; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } /** * 存款 * * @param x 欲存款金額 * */ public void oper(int x) { try { Thread.sleep(10L); synchronized (this) { this.cash += x; System.out.println("線程" + Thread.currentThread().getName() + "運行結束,增加“" + x + "”,當前用戶賬戶余額為:" + cash); } Thread.sleep(10L); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString() { return "User{" + "code='" + code + '\'' + ", cash=" + cash + '}'; } } }
線程線程B運行結束,增加“-60”,當前用戶賬戶余額為:40 線程線程A運行結束,增加“20”,當前用戶賬戶余額為:60 線程線程C運行結束,增加“-80”,當前用戶賬戶余額為:-20 線程線程D運行結束,增加“-30”,當前用戶賬戶余額為:-50 線程線程F運行結束,增加“21”,當前用戶賬戶余額為:-29 線程線程E運行結束,增加“32”,當前用戶賬戶余額為:3