简体   繁体   English

锁定Java多线程无法正常工作

[英]lock in java multithreading is not working

  //I have this main class

    package IntroductionLocks;

    public class Intro {

        public static void main(String[] args) {
            NoLockATM noLockATM = new NoLockATM();
            LockedATM lockedATM = new LockedATM();
            MyClass thread1 = new MyClass(noLockATM, lockedATM);
            MyClass thread2 = new MyClass(noLockATM, lockedATM);

            thread1.start();
            thread2.start();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            thread1.waitUntilDone();
            thread2.waitUntilDone();

            System.out.println("NoLock ATM: " + noLockATM.getBalance());
            System.out.println("Locked ATM: " + lockedATM.getBalance());
            int v = thread1.delta + thread2.delta + 100;
            System.out.println("Should Be: " + v);
            System.out.println("Program terminating.");
        }

    }


    //// 2nd class

    package IntroductionLocks;

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    import CtCILibrary.AssortedMethods;

    public class MyClass extends Thread  {
        private NoLockATM noLockATM;
        private LockedATM lockedATM;
        public int delta = 0;

        private Lock completionLock;

        public MyClass(NoLockATM atm1, LockedATM atm2) {
            noLockATM = atm1;
            lockedATM = atm2;
            completionLock = new ReentrantLock();
        }

        public void run() {
    //question here
            completionLock.lock();
            int[] operations = {10,20};//AssortedMethods.randomArray(20, -50, 50);
            for (int op : operations) {
                System.out.println(Thread.currentThread().getName());
                delta += op;
                if (op < 0) {
                    int val = op * -1;
                    noLockATM.withdraw(val);
                    lockedATM.withdraw(val);
                } else {
                    noLockATM.deposit(op);
                    lockedATM.deposit(op);              
                }
            }
            completionLock.unlock();
        }

        public void waitUntilDone() {
            completionLock.lock();
            completionLock.unlock();
        }
    }


//// 3rd class LockedATM

package IntroductionLocks;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockedATM {
    private Lock lock;
    private int balance = 100;

    public LockedATM() {
        lock = new ReentrantLock();
    }

    public int withdraw(int value) {
        lock.lock();
        int temp = balance;
        try {
            Thread.sleep(100);
            temp = temp - value;
            Thread.sleep(100);
            balance = temp;
        } catch (InterruptedException e) {      }
        lock.unlock();
        return temp;
    }

    public int deposit(int value) {
        lock.lock();
        int temp = balance;
        try {
            Thread.sleep(100);
            temp = temp + value;
            Thread.sleep(100);
            balance = temp;
        } catch (InterruptedException e) {      }
        lock.unlock();
        return temp;
    } 

    public int getBalance() {
        return balance;
    }
}

my question is...why completionLock.lock() in run method is not locking the resource. 我的问题是...为什么run方法中的completionLock.lock()没有锁定资源。 When i run the program, in System.out.println(Thread.currentThread().getName()) 当我运行程序时,在System.out.println(Thread.currentThread()。getName())中

i get below output: Thread-1 Thread-0 Thread-0 Thread-1 NoLock ATM: 130 Locked ATM: 160 Should Be: 160 Program terminating. 我得到以下输出:线程1线程0线程0线程1 NoLock ATM:130锁定的ATM:160应该是:160程序终止。

    `enter code here`isnt lock supposed to lock the resource....that mean only one thread can get access to it at a time.....????? then why it is showing that first thread 1 is getting acces then thread 0 then again thread 0 and then thread1 ???

   Isnt only thread1/0 should get first completed than other??

Also what is wait until done is supposed to do??? 还等什么应该做呢???

Each of your runnables has it's own lock object. 您的每个可运行对象都有其自己的锁定对象。 That is the answer. 那就是答案。 You need to have a shared lock. 您需要具有共享锁。 Or use one of your ATM objects as lock 或使用您的ATM对象之一作为锁

The problem is with the usage of the Reentrant lock. 问题在于重入锁的用法。 In your case, each instance of MyClass thread will have its own instance of the completionLock. 在您的情况下,MyClass线程的每个实例将具有其自己的完成锁实例。 For you to synchronize the 2 instances of MyClass thread you should be using a shared object. 为了使MyClass线程的2个实例同步,您应该使用共享对象。 Create the completionLock instance in the main method and pass the instance to both the threads 在main方法中创建完成锁实例,并将该实例传递给两个线程

new MyClass(noLockATM, lockedATM, completionLock);

public MyClass(NoLockATM atm1, LockedATM atm2, ReentrantLock completionLockArg) {
         this.noLockATM = atm1;
         this.lockedATM = atm2;
         this.completionLock = completionLockArg; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM