可以用java自动创建线程吗?根据标准 import java.util.*; 公开课考试{ 公开考试(){ int[]list=新的int[]{1,2,3,4,5,6}; ArrayList myList=新数组.asList(列表); //列出6个需要3个线程的编号 } 公共静态void main(字符串[]args){ 新测试(); } }
对于列表中的每两个数字,我希望有一个线程。i、 e如果它们是6个数字,则应自动确定3个线程。我必须如何自动创建线程?是。普遍接受的方法是生成可运行的,并将该可运行的提交给ExecutorService,以便为您管理线程。不要只是执行一个松散的线程。这是一个很糟糕的习惯,它让你无法控制线程可以用java自动创建线程吗?根据标准 import java.util.*; 公开课考试{ 公开考试(){ int[]list=新的int[]{1,2,3,4,5,6}; ArrayList myList=新数组.asList(列表); //列出6个需要3个线程的编号 } 公共静态void main(字符串[]args){ 新测试(); } },java,multithreading,Java,Multithreading,对于列表中的每两个数字,我希望有一个线程。i、 e如果它们是6个数字,则应自动确定3个线程。我必须如何自动创建线程?是。普遍接受的方法是生成可运行的,并将该可运行的提交给ExecutorService,以便为您管理线程。不要只是执行一个松散的线程。这是一个很糟糕的习惯,它让你无法控制线程 import java.util.*; public class test{ public test(){ int[] list = new int[]{1,2,3,4,5,6};
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();
}
}
只需确保当您处理完执行器(或在程序终止时)时,您执行了
ExecutorService service = Executors.newFixedThreadPool(5);
for (blah blahblah) {
Runnable runnable = new Runnable() {
....
}
service.execute(runnable);
}
或
也许您可以在
新数组之后添加一个判断,判断容器的大小是否是2的倍数代码>或每次添加到此容器时。
如果它不是2
的倍数,并且它不小于2
,那么您可以执行-1
,并将可以形成一对的先前玩家放入线程中。
如果列表上有其他语句,也可以遵循此逻辑
service.shutdownNow();
int[]list=newint[]{1,2,3,4,5,6};
ArrayList myList=新数组.asList(列表);
//列出6个需要3个线程的编号
if(myList.size()%2==0){//如果它是2的倍数
对于(inti=0;iimportjava.util.*
公开课考试{
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
if(myList.size() % 2 == 0){//if it is a multiple of 2
for(int i=0;i<myList.size()/2;i++){
//your code
new Thread(new YourThread()).start();
}
}else{//if it isn't a multiple of 2
for(int i=0;i<(myList.size()-1)/2 || myList.size()>2;i++){
//e.g: myList.size()=7 7-1=6 6/2=3
//your code
new Thread(new YourThread()).start();
}
}
公共无效测试(){
int[]list=新的int[]{1,2,3,4,5,6};
List threadList=new ArrayList();
对于(int i=0;i
}一个创建线程的简单循环怎么样?我不明白你在这里面临什么问题。这些线程应该做什么?这回答了你的问题吗?六个值怎么需要三个线程?每个线程对其“两个值”做什么?这只是一个简单的列表来说明我的问题。但是在现实生活中le,我在列表中有6个游戏玩家实例。其中两个用一个线程互相攻击。但是如果我想再添加2个玩家,使其成为8个,我必须手动键入另一个线程。start()对于它。有没有一种方法可以为列表中的每两个玩家自动生成线程?手动创建线程没有错。事实上,这样做至少比ExecutorService有一个显著的优势。我坚决不同意。启动新线程()允许对线程进行零管理或控制。您可以在小型应用程序中使用它,但任何更大的应用程序都会带来麻烦。这是一个坏习惯,应该避免。您知道可以将线程存储在变量和字段中,对吧?我一直都在这样做。例如,私有线程backgroundTask;
。或者您不这样做谈论线程池之类的东西?我当然不会尝试实现自己的线程池。
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
if(myList.size() % 2 == 0){//if it is a multiple of 2
for(int i=0;i<myList.size()/2;i++){
//your code
new Thread(new YourThread()).start();
}
}else{//if it isn't a multiple of 2
for(int i=0;i<(myList.size()-1)/2 || myList.size()>2;i++){
//e.g: myList.size()=7 7-1=6 6/2=3
//your code
new Thread(new YourThread()).start();
}
}
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();
}