Java MultiThreading [英] Java MultiThreading

查看:54
本文介绍了Java MultiThreading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的Java程序。我正在使用MultiThreading。

我实现了一个Runnable接口。

例如:


myRunnable实现Runnable {


myRunnable(int i,int j){

//做soemthing

}


run(){

//做点什么
}


}


Main(向量v){

int size = v。尺寸(); //(即10)

myRunnable [] coco = null;


for(int i = 0; i< 3; i ++){

coco [i] = new myRunnable(i,j);

coco [i] .start();

}

}


我的查询是...我有3个线程的限制要创建,但我必须运行所有10个对象。现在有什么方法可以在线程中使用Recurssion

例如:


run(){

if(something){

//更改线程中的值

start()

}

}

I have a complex Java program. I am using MultiThreading.
I implement a Runnable interface.
for example:

myRunnable implements Runnable{

myRunnable(int i, int j){
//do soemthing
}

run(){
// do something
}

}

Main(vector v ){
int size = v.size(); //(which is 10)
myRunnable[] coco = null;

for(int i = 0;i<3;i++){
coco[i] = new myRunnable (i, j);
coco[i].start();
}
}


my query is...i have limit of 3 thread to be created but i have to run all the 10 objects. Now is there any way of Recurssion in Threads
For example :

run(){
if(something){
//change values in thread
start()
}
}

推荐答案


我有一个复杂的Java程序。我正在使用MultiThreading。

我实现了一个Runnable接口。

例如:


myRunnable实现Runnable {


myRunnable(int i,int j){

//做soemthing

}


run(){

//做点什么
}


}


Main(向量v){

int size = v。尺寸(); //(即10)

myRunnable [] coco = null;


for(int i = 0; i< 3; i ++){

coco [i] = new myRunnable(i,j);

coco [i] .start();

}

}


我的查询是...我有3个线程的限制要创建,但我必须运行所有10个对象。现在有什么方法可以在线程中使用Recurssion

例如:


run(){

if(something){

//更改线程中的值

start()

}

}
I have a complex Java program. I am using MultiThreading.
I implement a Runnable interface.
for example:

myRunnable implements Runnable{

myRunnable(int i, int j){
//do soemthing
}

run(){
// do something
}

}

Main(vector v ){
int size = v.size(); //(which is 10)
myRunnable[] coco = null;

for(int i = 0;i<3;i++){
coco[i] = new myRunnable (i, j);
coco[i].start();
}
}


my query is...i have limit of 3 thread to be created but i have to run all the 10 objects. Now is there any way of Recurssion in Threads
For example :

run(){
if(something){
//change values in thread
start()
}
}



1.)每次发布代码时请使用代码标签。

2.)vector v必须与线程有什么关系?在你发布的代码中(显然没有编译),没有办法告诉你要做什么。也许如果你再次描述你的问题或发布编译和工作的代码。

1.) Please use code tags everytime when posting code.
2.) What does vector v have to to with the threads? In the codes that you have posted (which obviously don''t compile) there is no way to tell what you are trying to do. Perhaps if you describe your problem again or post the codes that are compiling and working.


我读你的问题的方式是:有三个线程和十个向量

需要同时由三个线程操纵。我假设当

一个向量被一个(单个!)线程操纵时,它不需要再被操纵

。我会实现一个共享资源:一组要操纵的向量。

每个线程竞争从该包中获取和删除一个向量。一旦从包中获取了一个向量

(并且被删除),该线程就会操纵它并尝试从包中获取下一个向量。当包是空的时候,线程就会死掉。


Bag对象中的一些同步方法(共享资源)可以为你完成

的工作。


亲切的问候,


Jos
The way I read your question is: there are three threads and ten vectors that
need to be manipulated by three threads at the same time. I assume that when
a vector has been manipulated by a (single!) thread it doesn''t need to be manipulated
again. I''d implement a shared resource: a bag of vectors to be manipulated.
Each thread competes to get and remove a vector from that bag. Once a vector
is obtained from the bag (and removed), the thread manipulates it and tries to
obtain a next vector from the bag. When the bag is empty, the thread dies.

A few synchronized methods in the Bag object (the shared resource) can do the
job for you.

kind regards,

Jos



我读你的问题的方法是:有三个线程和十个向量,需要同时由三个线程操纵
。我假设当

一个向量被一个(单个!)线程操纵时,它不需要再被操纵

。我会实现一个共享资源:一组要操纵的向量。

每个线程竞争从该包中获取和删除一个向量。一旦从包中获取了一个向量

(并且被删除),该线程就会操纵它并尝试从包中获取下一个向量。当包是空的时候,线程就会死掉。


Bag对象中的一些同步方法(共享资源)可以为你完成

的工作。


亲切的问候,


Jos
The way I read your question is: there are three threads and ten vectors that
need to be manipulated by three threads at the same time. I assume that when
a vector has been manipulated by a (single!) thread it doesn''t need to be manipulated
again. I''d implement a shared resource: a bag of vectors to be manipulated.
Each thread competes to get and remove a vector from that bag. Once a vector
is obtained from the bag (and removed), the thread manipulates it and tries to
obtain a next vector from the bag. When the bag is empty, the thread dies.

A few synchronized methods in the Bag object (the shared resource) can do the
job for you.

kind regards,

Jos



这提出了一个问题:如果两个线程试图读取和写入一个向量,会发生什么?


这种让我想到我过去的类,时钟的原因以及各个组件之间的主从关系。


难道不应该设置程序/线程,以便它们不能同时操纵同一组数据吗?这是否已经通过java以某种方式实现了?


感谢您的知识,


-blazed

This brings up a question: if two threads are attempting to read and write a vector, what happens?

This kind of makes me think about my past classes, the reason for clocking and master-slave relationships between various components.

Shouldn''t the program/threads be set up so that they can''t manipulate the same set of data at any one time? Is this already implemented somehow through java?

Thanks for the knowledge,

-blazed


这篇关于Java MultiThreading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆