- class Account
- {
- .....
-
- public synchronized void draw(double drawAmount)//当两个线程执行到此处时,持有的是同一个锁吗
- {
- ....
- }
- }
- class Customer extends Thread
- {
- private Account account;
- ...
- public void run(){
- for (int i=0;i<10;i++ )
- {
- account.draw(drawAmount);
- }
- }
- }
- class AccountThread
- {
- Account account = new Account(参数);
- //创建两个线程
- Customer c1 = new Customer(参数);
- Customer c2 = new Customer(参数);
- //开启两个线程
- c1.start();
- c2.start();
- }
复制代码
代码中的两个线程执行到draw(参数)方法时,持有的是同一个锁吗 |
|