【问题标题】:How to make long waiting thread to run如何使长时间等待的线程运行
【发布时间】:2018-05-04 12:51:11
【问题描述】:

我在核心 java 中工作。我有一个小程序,它将通过相应的线程 thread1 、 thread2 和 thread3 打印数字 1,2,3。

while(true)
        {
            synchronized (obj)
            {

                System.out.println("Thread got chance : "+Thread.currentThread().getName());


                if (ai.get() ==0 && Thread.currentThread().getName().equalsIgnoreCase("Thread1"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    obj.notify();
                    obj.wait();
                }

                if (ai.get() ==1 && Thread.currentThread().getName().equalsIgnoreCase("Thread2"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    obj.notify();
                    obj.wait();
                }

                if (ai.get() ==2 && Thread.currentThread().getName().equalsIgnoreCase("Thread3"))
                {
                    System.out.print(ai.incrementAndGet()+"("+Thread.currentThread().getName()+")"+" ");
                    System.out.println("");
                    ai.set(0);
                    obj.notify();
                    obj.wait();
                }

            }

        }

上面的程序逻辑很好并且可以正常工作,即它正在按顺序打印。但是我已经用“Thread got change”打印了线程名称。即我试图确定哪个线程有更多机会运行并知道线程名称 Ex thread 2。

问题是“我怎样才能确保所有线程都获得相同的机会。由于这是小程序,线程将在几毫秒内完成它的工作并出来,我们不会知道。如果线程需要很长时间才能运行”?

请帮忙。

【问题讨论】:

    标签: java multithreading concurrency synchronized reentrantlock


    【解决方案1】:

    synchronized 不支持 fair 策略。当您调用notify() 时,任何等待线程都可能被唤醒。

    您可以使用公平的ReentrantLock

    • 调用lock()时,等待时间最长的线程将获取锁。
    • 当您调用signal() 时,将首先发出等待时间最长的线程的信号。

    这是示例代码:

    // Use the same lock and condition in different threads
    ReentrantLock lock = new ReentrantLock(true);  // create a fair lock
    Condition condition = lock.newCondition();
    
    while (true) {
        lock.lock();
        try {
            System.out.println("Thread got chance : " + Thread.currentThread().getName());
    
            if (ai.get() == 0 && Thread.currentThread().getName().equalsIgnoreCase("Thread1")) {
                System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
                condition.signal();
                condition.await();
            }
    
            if (ai.get() == 1 && Thread.currentThread().getName().equalsIgnoreCase("Thread2")) {
                System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
                condition.signal();
                condition.await();
            }
    
            if (ai.get() == 2 && Thread.currentThread().getName().equalsIgnoreCase("Thread3")) {
                System.out.print(ai.incrementAndGet() + "(" + Thread.currentThread().getName() + ")" + " ");
                System.out.println("");
                ai.set(0);
                condition.signal();
                condition.await();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多