为什么要用static修饰Lock对象

package java_Thread;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;

import java_Thread.AccountWithSyncUsingLock.Account;

public class ThreadCooperation {
    private static Account account = new Account();
    
    public static void main(String[] args){
        ExecutorService executor = Executors.newFixedThreadPool(2);
        executor.execute(new DepositTask());
        executor.execute(new WithdrawTask());
        executor.shutdown();
        
        System.out.println("Thread 1\t\tThread 2\t\tBalance");
    }
    
    public static class DepositTask implements Runnable{
        public void run(){
            try{
                while(true){
                    account.deposit((int)(Math.random()*10+1));
                    Thread.sleep(1000);
                }
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
        }
    }
    
    public static class WithdrawTask implements Runnable{
        public void run(){
            while(true){
                account.withdraw((int)(Math.random()*10)+1);
            }
        }
    }
    
    private static class Account{
        private static Lock lock = new ReentrantLock();
        private static Condition newDeposit = lock.newCondition();
        private int balance = 0;
        
        public int getBalance(){
            return balance;
        }
        
        public void withdraw(int amount){
            lock.lock();
            try{
                while(balance < amount){
                    System.out.println("\t\t\tWait for a deposit");
                    newDeposit.await();
                }
                balance -=amount;
                System.out.println("\t\t\tWithdraw "+ amount+"\t\t" + getBalance());
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
            finally{
                lock.unlock();
            }
        }
        
        public void deposit(int amount){
            lock.lock();
            try{
                balance += amount;
                System.out.println("Deposit "+ amount + "\t\t\t\t\t" + getBalance());
                
                newDeposit.signalAll();
            }
            finally{
                lock.unlock();
            }
        }
    }
}

最近在学习JAVA的多线程,这是书上的例子。为什么声明Lock时要用static?这个例子中只创建了一个Account实例,但假如有多个Account实例的话,按道理它们之间应该是互不干扰的,但是Lock对象声明为类变量的话是不是逻辑就不对了?

阅读 7.9k
3 个回答

从整体的代码来说这里的static是多余的,因为ThreadCooperation中的Account对象是static的,而且Account中的balance是非static的,所以如果想要达成代码中的效果,其实Lock和Condition前面的static反而会造成误导。

我也觉得这个 static 并非必须。lock 的作用域定义在什么范围,这个根据业务逻辑的需要来定。在楼主的例子中,lock 被声明为静态的,意味着不论访问哪个 Account 对象,都会被全局的同步;如果去掉 static,就意味着有多少个 Account 对象就会有多少个 lock,访问不同的 Account 对象的线程之间不会互相干扰。

static 代表是公用的。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题