以迭代方式使用Java中的线程
我想知道是否有人能帮我解决一个问题 我想要完成的(使用Java): 在第一次迭代中,我想“doStuff”10次(同时) 在第二次迭代中,我希望在第一次迭代完成后“doStuff”20次(以并发方式) 等等。。。(我希望能够循环,因为我计划重复100次) 我的代码的问题是它一次做了30次。任何帮助都将不胜感激 PS:为了简化,部分代码被删除了,所以如果有任何错误,请告诉我以迭代方式使用Java中的线程,java,multithreading,for-loop,Java,Multithreading,For Loop,我想知道是否有人能帮我解决一个问题 我想要完成的(使用Java): 在第一次迭代中,我想“doStuff”10次(同时) 在第二次迭代中,我希望在第一次迭代完成后“doStuff”20次(以并发方式) 等等。。。(我希望能够循环,因为我计划重复100次) 我的代码的问题是它一次做了30次。任何帮助都将不胜感激 PS:为了简化,部分代码被删除了,所以如果有任何错误,请告诉我 public static void doStuff(){ [Code] } public static void
public static void doStuff(){
[Code]
}
public static void threadThis (int y){
for (int i = 0; i<y; i++) {
Thread t1 = new Thread(){
public void run() {
doStuff();
}
};
t1.start();
}
}
public static void main(String[] agrs) throws InterruptedException {
for (int p = 0; p<21; p = p + 10){
threadThis(p);
}
}
publicstaticvoiddostuff(){
[守则]
}
公共静态无效线程(整数y){
对于(inti=0;i,这里是一个很好的任务,用于java.util.concurrent包中的CountDownLatch类
当然,你的任务被分成了两个应该排队的大任务,这就是倒计时
查找文档,这里有一个很好的代码示例,可以很容易地根据您的需要采用这里是一个很好的任务,用于java.util.concurrent包中的CountDownLatch类
当然,你的任务被分成了两个应该排队的大任务,这就是倒计时
查找文档,这里有一个很好的代码示例,可以很容易地根据您的需要采用这里是一个很好的任务,用于java.util.concurrent包中的CountDownLatch类
当然,你的任务被分成了两个应该排队的大任务,这就是倒计时
查找文档,这里有一个很好的代码示例,可以很容易地根据您的需要采用这里是一个很好的任务,用于java.util.concurrent包中的CountDownLatch类
当然,你的任务被分成了两个应该排队的大任务,这就是倒计时
查找文档,这里有一个很好的代码示例,可以方便地根据您的需要采用您可以在启动线程后将threadThis()更改为join()所有线程
public static void threadThis (int y){
List<Thread> threads = Lists.newArrayListWithCapacity(y);
for (int i = 0; i<y; i++) {
Thread t1 = new Thread(){...};
t1.start();
threads.add(t1);
}
for (Thread t : threads) {
t.join();
}
}
publicstaticvoidthreadthis(int-y){
List threads=Lists.newArrayListWithCapacity(y);
对于(int i=0;i您可以将threadThis()更改为join(),以便在它启动所有线程后将其连接起来
public static void threadThis (int y){
List<Thread> threads = Lists.newArrayListWithCapacity(y);
for (int i = 0; i<y; i++) {
Thread t1 = new Thread(){...};
t1.start();
threads.add(t1);
}
for (Thread t : threads) {
t.join();
}
}
publicstaticvoidthreadthis(int-y){
List threads=Lists.newArrayListWithCapacity(y);
对于(int i=0;i您可以将threadThis()更改为join(),以便在它启动所有线程后将其连接起来
public static void threadThis (int y){
List<Thread> threads = Lists.newArrayListWithCapacity(y);
for (int i = 0; i<y; i++) {
Thread t1 = new Thread(){...};
t1.start();
threads.add(t1);
}
for (Thread t : threads) {
t.join();
}
}
publicstaticvoidthreadthis(int-y){
List threads=Lists.newArrayListWithCapacity(y);
对于(int i=0;i您可以将threadThis()更改为join(),以便在它启动所有线程后将其连接起来
public static void threadThis (int y){
List<Thread> threads = Lists.newArrayListWithCapacity(y);
for (int i = 0; i<y; i++) {
Thread t1 = new Thread(){...};
t1.start();
threads.add(t1);
}
for (Thread t : threads) {
t.join();
}
}
publicstaticvoidthreadthis(int-y){
List threads=Lists.newArrayListWithCapacity(y);
对于(int i=0;i,正如其他人所说,CountDownLatch
的概念在这里非常有用。CountDownLatch
实际上使用了wai
t和notify
来实现类似的功能
Main
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
经理
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
正如其他人所说,CountDownLatch
的概念在这里非常有用。CountDownLatch
实际上是在下面使用wai
t和notify
。因此,这就是如何让wait
和notif
y工作并自己实现类似功能的方法
Main
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
经理
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
正如其他人所说,CountDownLatch
的概念在这里非常有用。CountDownLatch
实际上是在下面使用wai
t和notify
。因此,这就是如何让wait
和notif
y工作并自己实现类似功能的方法
Main
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
经理
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
正如其他人所说,CountDownLatch
的概念在这里非常有用。CountDownLatch
实际上是在下面使用wai
t和notify
。因此,这就是如何让wait
和notif
y工作并自己实现类似功能的方法
Main
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
经理
public class Main
{
public static void main(String[] args)
{
Manager mgr = new Manager("manager");
mgr.start();
}
}
public class Manager extends Thread
{
public int threadCount=0;
private final Object lock = new Object();
public Manager(String name){
super(name);
}
public void pauseAndWaitForAll()
{
synchronized (this.lock) {
try {
while(this.threadCount>0)
{
this.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void countDown()
{
synchronized (this.lock) {
this.threadCount--;
// if the if block becomes uncommented notify will only be called
// by last running thread of group, thus will only be called by last running thread of
// group. If the if block is left commented out then every thread that is done with its
// work will call notify and thus wait will be called again due to the
// while(thread.count>0) loop not terminating
//if (this.threadCount <= 0)
//{
this.lock.notify();
//}
}
}
public void run ()
{
for (int p = 10; p<1001; p = p + 10)
{
this.threadCount=p;
for (int i = 0; i<p; i++)
{
System.out.println(" new thread...: "+i+" ");
new DoWork(this,"thread: num:"+i+"--group:"+p).start();
}
pauseAndWaitForAll();
// uncomment the following for debugging
// System.out.println(" ------------------next group-----------------------");
// try {
// Thread.sleep( 3000 );
// } catch (InterruptedException e) {
// System.out.println(" pause error ");
// }
}
}
}
public class DoWork extends Thread
{
private Manager managerThread;
public DoWork(Manager managerThread,String name){
super(name);
this.managerThread=managerThread;
}
public void run ()
{
try {
// System.out.print(this.getName()+" going to sleep ...: ");
int randSleep= 1000*(0 + (int)(Math.random() * ((5 - 0) + 1)));
Thread.sleep(randSleep);
System.out.println(this.getName()+" woke up ... ");
} catch (InterruptedException e) {
System.out.println(" worker thread: job simulation error:"+e);
}
managerThread.countDown();
}
}
如果你想让20个“doStuff”的第二次迭代在第一次迭代之后运行,你需要在线程的第二次迭代上实现一个hold。现在没有什么可以做的,所以所有30个线程在调用时都在运行和执行。看看教程。创建你自己的类来扩展Thread类,只要“dostuf()”函数finishs设置一个布尔finished=true。您可以在循环中检查所有当前正在运行的线程,如果finish属性都为true,您可以再创建10个线程来执行其他任务。使用公共静态同步线程是否会使此
无效?@Roberto很遗憾,这不起作用。结果相同。如果您想要第二个i要在第一次迭代后运行20个“doStuff”,您需要在线程的第二次迭代上实现一个hold。现在没有任何操作,因此调用时所有30个线程都在运行和执行。请查看教程。创建自己的类来扩展Thread类,并在“doStuff()”函数finishs设置一个布尔finished=true。您可以在循环中检查所有当前正在运行的线程,如果finish属性都为true,您可以再创建10个线程来执行其他任务。使用公共静态同步线程是否会使此
无效?@Roberto很遗憾,这不起作用。结果相同。如果您想要第二个i畸胎