gpt4 book ai didi

java - 如何让特定线程 hibernate ?

转载 作者:行者123 更新时间:2023-11-30 03:53:46 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com