java - 即使第一个线程使用 ReentrantLock 锁定,第二个线程也会执行

标签 java multithreading thread-synchronization reentrantlock

我是 Java 新手,正在尝试通过实现来学习 Java 概念。 这里之所以有ReentrantLock类,是为了理解锁。

我正在生成 3 个线程,并且在这些线程中我只是增加了一个全局计数器。 我使用锁保护计数器被其他线程覆盖。

import java.util.concurrent.locks.ReentrantLock;

class ReentryHandledSingleThread extends Thread
{
    static long counter = 0;

    private int myId;

    private final ReentrantLock myLock = new ReentrantLock();

    public ReentryHandledSingleThread(int id)
    {
        this.myId = id;
    }

    public void incrementTheCounter()
    {

        long stackvariable;
        int i;

        for (i = 0; i < 10000; i++)
        {
            stackvariable = ReentryHandledSingleThread.counter;
            stackvariable = stackvariable + 1;
            ReentryHandledSingleThread.counter = stackvariable;
        }
        System.out.println("The value from counter is " + ReentryHandledSingleThread.counter);
        return;

    }

    public void run()
    {
        System.out.println("Started Thread No. " + this.myId);
        this.myLock.lock();
        {
            System.out.println("LOCKED Thread No. " + this.myId);
            this.incrementTheCounter();
        }
        System.out.println("UNLOCKED   Thread No." + this.myId);
        this.myLock.unlock();

    }
}

public class RentryHandle
{

    public static void main(String[] args)
    {
        System.out.println("Started Executing Main Thread");
        int noOfThreads = 3;
        ReentryHandledSingleThread threads[] = new ReentryHandledSingleThread[noOfThreads];
        for (int j = 0; j < noOfThreads; j++)
        {
            threads[j] = new ReentryHandledSingleThread(j);
        }

        for (int j = 0; j < noOfThreads; j++)
        {
            threads[j].start();
        }

        for (int j = 0; j < noOfThreads; j++)
        {
            try

            {
                threads[j].join();
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        System.out.println("Finished Executing Main thrread");

    }
}

从上述代码观察到的输出

    Started Executing Main Thread 
    Started Thread No. 0 
    LOCKED Thread No.   0 
    Started Thread No. 2 
    LOCKED Thread No. 2 
    The value from counter is   10226 
    UNLOCKED   Thread No.0 
    The value from counter is 16165 
    UNLOCKED   Thread No.2 
    Started Thread No. 1 
    LOCKED Thread No. 1 
    The value from   counter is 26165 
    UNLOCKED   Thread No.1 
    Finished Executing Main   thrread

我的预期输出

    Started Executing Main Thread
    Started Thread No. 0
    LOCKED Thread No. 0
    The value from counter is 10000
    UNLOCKED   Thread No.0
    Started Thread No. 1
    LOCKED Thread No. 1
    The value from counter is 20000
    UNLOCKED   Thread No.1
    Started Thread No. 2
    LOCKED Thread No. 2
    The value from counter is 30000
    UNLOCKED   Thread No.2
    Finished Executing Main thrread

我浏览了reentrantlock-lock-doesnt-block-other-threads 但是,我不在这里使用

Condition.await()

因此我无法与我的实现相关联。 请帮助我理解我的实现中的错误或理解 ReentrantLock 应用程序,该应用程序导致预期输出和观察到的输出存在差异。

最佳答案

问题是每个线程对象都有自己的锁(并且能够独立于所有其他线程正在执行的操作来锁定它):

private final ReentrantLock myLock = new ReentrantLock();

如果您希望跨线程共享锁,请将上述对象设为静态

关于java - 即使第一个线程使用 ReentrantLock 锁定,第二个线程也会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26229387/

相关文章:

java - Axis2 响应中丢失附件

java - SoapFaultClientException : outputting detail

c++ - 在两个线程都在使用时将一个线程移动到另一个线程

iOS - GCD 和 __strong 引用

两个线程可以使用相同的线程过程吗?

java - GUI RadioButtonJPanel

java - 如何使用 jSTL 从 url 获取一些值

c# - 运行多个 UI 线程

java - 无法理解 java 中的同步