如何让一个特定的线程睡觉?

时间:2014-05-20 22:36:27

标签: java multithreading thread-sleep

我有9个线程进行一些计算。我想让他们中的一个或更多人去睡觉。

代码:

public class CalcArray 
{
    private static int[] m_array = null;
    private static int sum = 0;
    private final static int MAX_VALUE = 80;

    /**
     * 
     */
    public static void putValues()
    {
        for (int i = 0; i < MAX_VALUE ; i++)
        {
            m_array[i] = 1;
        }
    }


    /**
     * 
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException 
    {
        m_array = new int[80];
        putValues();
        int lowBound = 0;
        int upperBound = 9;

        Thready[] threadsArray = new Thready[10]; 
        for (int j = 0; j < 8 ; j++)
        {
            threadsArray[j] = new Thready(lowBound , upperBound);
            lowBound += 10;
            upperBound += 10;
        }

        // start the threads

        for (int q = 0; q < 8; q++)
        {
            if (q == 5)
            {
                // make the thread with #5 to take a nap
                // Thread.sleep(3000);
            }
            threadsArray[q].run();
        }

        System.out.println("Main Thread is done!");
        System.out.println(sum);

    }


    /**
     * 
     * @author X
     *
     */
    public static class Thready implements Runnable 
    {
        int lower = 0;
        int upper = 0;

        public Thready(int paramLower , int paramUpper)
        {
            lower = paramLower;
            upper = paramUpper;
        }

        @Override
        public void run() 
        {
            synchronized(m_array)
            {
                for (int i = lower ; i <= upper ; i++)
                {
                    sum += m_array[i];
                }

                System.out.println("The current value is :" + sum);
            }

        }
    }

}

我想让第5号线号小睡一下。如果我使用Thread.sleep(3000);,它将导致主线程进入睡眠状态。那么我怎么能告诉线程#5进入睡眠状态呢?

1 个答案:

答案 0 :(得分:2)

像这样:

public class Test 
{
    private static int[] m_array = null;
    private static int sum = 0;
    private final static int MAX_VALUE = 80;

    /**
     * 
     */
    public static void putValues()
    {
        for (int i = 0; i < MAX_VALUE ; i++)
        {
            m_array[i] = 1;
        }
    }


    /**
     * 
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException 
    {
        m_array = new int[80];
        putValues();
        int lowBound = 0;
        int upperBound = 9;

        Thready[] threadsArray = new Thready[10]; 
        for (int j = 0; j < 8 ; j++)
        {
            threadsArray[j] = new Thready(j, lowBound , upperBound);
            lowBound += 10;
            upperBound += 10;
        }

        // start the threads

        for (int q = 0; q < 8; q++)
        {
            if (q == 5)
            {
                threadsArray[q].snooze(3000);
                // make the thread with #5 to take a nap
                // Thread.sleep(3000);
            }
            (new Thread(threadsArray[q])).start();
        }

        System.out.println("Main Thread is done!");
        System.out.println(sum);

    }


    /**
     * 
     * @author X
     *
     */
    public static class Thready implements Runnable 
    {
        int lower = 0;
        int upper = 0;
        int threadNum = 0;

        public Thready(int threadNum, int paramLower , int paramUpper)
        {
            this.threadNum = threadNum;
            lower = paramLower;
            upper = paramUpper;
        }

        public void snooze(long howlong) throws InterruptedException
        {
            System.out.println("Thread "+threadNum+": Taking a nap of "+howlong+" millis.");
            Thread.sleep(howlong);
        }

        @Override
        public void run() 
        {
            synchronized(m_array)
            {
                for (int i = lower ; i <= upper ; i++)
                {
                    sum += m_array[i];
                }

                System.out.println("Thread "+threadNum+": The current value is :" + sum);
            }

        }
    }

}