作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 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 去 sleep 呢?
最佳答案
像这样:
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);
}
}
}
}
关于java - 如何让特定线程 hibernate ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771124/
我是一名优秀的程序员,十分优秀!