簡體   English   中英

多線程-如何同時獲取所有線程

[英]Multithreading - how to get all threads ticking at the same time

好的,我有一個運行某些代碼的客戶線程。 但是我希望它在完成將計數器減少到零的特定方法(setShopTime和setCheckoutTime)后進入睡眠狀態。 在計數器的每個遞減滴答聲中,我希望客戶停止,直到其他所有線程的滴答聲也都減少1。但是請記住,並不是所有客戶都將使用與兩個相同的方法來滴答。 這樣做的最好方法是在運行該方法后使每個線程休眠,讓另一個線程充當滴答計數器,然后在每個滴答之后對其他線程進行中斷以使它們再次移動嗎? 我只是擔心這個問題,因為如果一個客戶仍在運行並且滴答計數器線程中斷消失了,那可能會把事情弄糟嗎?

我將向您展示可奔跑的人,也許是比我更聰明的人,可以給我一些有關如何最好地解決此問題的提示。 我在想睡覺的地方發表了評論。

class customerRunnable implements Runnable {

private Customer customer;
private CheckoutFloor checkoutFloor;
private Lock customerLock;
private Condition customerReady;

public customerRunnable(CheckoutFloor checkoutFloor) {
    customer = new Customer();
    this.checkoutFloor = checkoutFloor;
}

public void run() {
        customer.addRandomShopTime();
        while (customer.shopTime > 0) {
            customer.setShopTime();
            //sleep
        }
        CheckoutOperator checkoutOperator = checkoutFloor.weightedCheckoutDeterminator();
        checkoutOperator.addCustomer(customer);

        while (customer.checkoutTime > 0) {
            if (checkoutOperator.customerList.get(0) == customer) {
                customer.setCheckoutTime();
                //sleep
            }
        }
        checkoutOperator.removeCurrentCustomer();

    } catch (InterruptedException e) {

    }
}

對此的修改版本可能會滿足您的需求:

public class Example
{
    int counter1 = 10;
    int counter2 = 10;

    public class Thread1 extends Thread
    {
        @Override
        public void run ()
        {
            try
            {
                while (Example.this.counter2 > 0)
                {
                    // Do your stuff

                    synchronized (Example.this)
                    {
                        Example.this.counter1--;
                        Example.this.notifyAll();
                        Example.this.wait ();
                    }
                }
            }
            catch (final InterruptedException e)
            {
            }
        }
    }

    public class Thread2 extends Thread
    {
        @Override
        public void run ()
        {
            try
            {
                while (Example.this.counter1 > 0)
                {
                    // Do your stuff

                    synchronized (Example.this)
                    {
                        Example.this.counter2--;
                        Example.this.notifyAll();
                        Example.this.wait ();
                    }
                }
            }
            catch (final InterruptedException e)
            {
            }
        }
    }
}

切勿使用sleep修改線程的行為。 JVM決定線程何時開始運行,您會發現自己只是增加了越來越長的睡眠時間,直到線程依次而不是同時運行。 即使它似乎偶爾工作,也不會是確定性的。

在您的代碼中,當您應阻止外部事件時,您的while循環會浪費CPU時間。 請看一下使用減少信號量計數器並在線程之間共享的信號量。

如果我正確理解您的意思,那么您想使用障礙。 這是帶有一些示例的CyclicBarrier的文檔: http : //docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM