Java 如何使特定的线程睡眠?
我有9个线程做一些计算。我想让他们中的一个或更多人睡觉 守则:Java 如何使特定的线程睡眠?,java,multithreading,thread-sleep,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() {
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);
}
}
}
}
公共类钙质
{
私有静态int[]m_数组=null;
私有静态整数和=0;
私有最终静态int MAX_值=80;
/**
*
*/
公共静态值()
{
对于(int i=0;i
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);
}
}
}
}
公共类测试
{
私有静态int[]m_数组=null;
私有静态整数和=0;
私有最终静态int MAX_值=80;
/**
*
*/
公共静态值()
{
对于(int i=0;i 对于(int i=lower;i),您不能使另一个线程睡眠。您只能使当前线程睡眠。该线程只能使自身睡眠。您需要以某种方式向其传递一个信号(在您自己的代码中实现)。不要使用run()方法启动线程,它只从该线程执行run方法,然后关闭。使用start()相反。你的示例中只有一个线程…@Niemand示例中没有线程,只有可运行线程,因此它们只能运行…你当然是对的,我错把Thready当成了thread。他应该使用类似new thread(new Thready())的东西。start();很好。我试图避免这种情况,我认为有一些内置的方法,但我们会设法:)。谢谢。