可以根据条件在Java中自动创建线程吗?

huangapple go评论78阅读模式
英文:

Is it possible to automatically create Threads in java? depending on criteria

问题

import java.util.*;

public class test {

    public test() {
        int[] list = new int[]{1, 2, 3, 4, 5, 6}; 
        ArrayList<Integer> myList = new Arrays.asList(list);
        //list with 6 numbers that require 3 threads           
    }

    public static void main(String[] args) {
        new test();
    }
}

每两个数字对应一个线程,例如如果有6个数字,则应自动创建3个线程。要如何实现自动创建线程?

英文:
import java.util.*;

public class test{


public test(){
     int[] list = new int[]{1,2,3,4,5,6}; 
     ArrayList&lt;Integer&gt; myList  = new Arrays.asList(list);
     //list with 6 numbers that require 3 threads           
  }
  
public static void main(String[] args){
     new test();

  

 }


}

For every two numbers in my list, i want to have a thread. i.e if their are 6 numbers, 3 threads should be detedted automatically. What must i do to automatically create Threads?

答案1

得分: 1

是的。一般被接受的方法是生成一个Runnable,然后将该Runnable提交给ExecutorService来管理线程。不要仅仅执行一个独立的线程。养成这种习惯是很糟糕的,它会让你无法控制线程。

ExecutorService service = Executors.newFixedThreadPool(5);
for (blah blahblah) {
    Runnable runnable = new Runnable() {
    ....
    }
    service.execute(runnable);
}

只要确保在使用完Executor之后(或在程序终止时),进行以下操作:

service.shutdown();

或者

service.shutdownNow();
英文:

Yes. The generally accepted method is to generate a Runnable and submit that Runnable to an ExecutorService to manage the threads for you. DON'T JUST EXECUTE A LOOSE THREAD. That's a terrible habit to get in and it makes it impossible for you to control the threads.

ExecutorService service = Executors.newFixedThreadPool(5);
for (blah blahblah) {
    Runnable runnable = new Runnable() {
    ....
    }
    service.execute(runnable);
}

Just be sure that when you're done with the executor (or at program termination), you do a

service.shutdown();

or

service.shutdownNow();

答案2

得分: 0

也许你可以在 new Arrays.asList(list); 之后或每次向容器中添加元素时,添加一个判断条件,判断容器的大小是否是 2 的倍数。
如果不是 2 的倍数且不小于 2,则可以执行 -1 操作,并将之前可以组成一对的玩家放入一个线程中。
如果列表上还有其他操作,也可以按照这个逻辑进行处理。

int[] list = new int[]{1,2,3,4,5,6};
ArrayList<Integer> myList = new Arrays.asList(list);

// 包含 6 个数字的列表,需要 3 个线程
if (myList.size() % 2 == 0) { // 如果是 2 的倍数
    for (int i = 0; i < myList.size() / 2; i++) {
        // 你的代码
        new Thread(new YourThread()).start();
    }
} else { // 如果不是 2 的倍数
    for (int i = 0; i < (myList.size() - 1) / 2 || myList.size() > 2; i++) {
        // 例如:myList.size()=7,7-1=6,6/2=3

        // 你的代码
        new Thread(new YourThread()).start();
    }
}
英文:

Maybe you can add a judgment wheather container's size is a multiple of 2 after new Arrays.asList(list); or every time you add to this container.
If it's not a multiple of 2 and it's not less than 2, then you can perform -1, and put the previous players who can form a pair in a thread.
If there are other statements that operate on the list, you can also follow this logic.

int[] list = new int[]{1,2,3,4,5,6}; 
	ArrayList&lt;Integer&gt; myList  = new Arrays.asList(list);
	//list with 6 numbers that require 3 threads           
	if(myList.size() % 2 == 0){//if it is a multiple of 2
		for(int i=0;i&lt;myList.size()/2;i++){
			//your code
			new Thread(new YourThread()).start();
		}
	}else{//if it isn&#39;t a multiple of 2
		for(int i=0;i&lt;(myList.size()-1)/2 || myList.size()&gt;2;i++){
			//e.g: myList.size()=7 7-1=6 6/2=3

			//your code
			new Thread(new YourThread()).start();
		}
	}

答案3

得分: 0

import java.util.*;

public class test{

    public void test(){
        int[] list = new int[]{1,2,3,4,5,6}; 
        List<Thread> threadList = new ArrayList<Thread>();
        for(int i =0; i < list.length/2; i++) {
            Thread thread = new Thread();
            threadList.add(thread);
        }         
    }

    public static void main(String[] args){
        new test();
    }
}
英文:

import java.util.*;

public class test{

public void test(){
    int[] list = new int[]{1,2,3,4,5,6}; 
    List&lt;Thread&gt; threadList = new ArrayList&lt;Thread&gt;();
    for(int i =0; i &lt; list.length/2; i++) {
        Thread thread = new Thread();
	    threadList.add(thread);
    }         
}

public static void main(String[] args){
    new test();
}

}

huangapple
  • 本文由 发表于 2020年3月16日 21:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定