java多线程处理任务 现在有一堆任务,要求10个线程同时执行,每个任务只能被一个线程执行,直到把所有任务执行完毕。怎么写啊。哪个大哥帮写一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 concurrent 包里有不少executor可以实现你的要求 创建一个线程池,然后执行任务ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 6, 3,TimeUnit.SECONDS, //缓冲队列为3 new ArrayBlockingQueue<Runnable>(3), //抛弃旧的任务 new ThreadPoolExecutor.DiscardOldestPolicy());threadPool.execute(Task task);Task类必须实现Runnable接口。如果共享数据 需要考虑同步问题 你的线程只要不结束他就可以了,写了个例子,希望对你有帮助package com.study.thread;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class ThreadRun { public static List<String> tasks = Collections.synchronizedList(new ArrayList<String>()); public static List<Th> threads = Collections.synchronizedList(new ArrayList<Th>()); static{ for(int i = 0; i < 100; i++){ tasks.add("任务" + i); } for(int i = 0; i < 10; i++){ Th th = new Th(); th.setName("任务线程" + i); th.start(); threads.add(th); } } public static void main(String[] args) { while(!tasks.isEmpty()){ String task = tasks.remove(0); if(!threads.isEmpty()){ Th t = threads.remove(0); t.setTask(task); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }}class Th extends Thread{ private String task; public void run(){ while(!ThreadRun.tasks.isEmpty()){ if(task != null){ System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......"); task = null; ThreadRun.threads.add(this); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void setTask(String task){ this.task = task; }} package com.study.thread;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class ThreadRun { public static List<String> tasks = Collections.synchronizedList(new ArrayList<String>()); public static List<Th> threads = Collections.synchronizedList(new ArrayList<Th>()); static{ for(int i = 0; i < 100; i++){ tasks.add("任务" + i); } for(int i = 0; i < 10; i++){ Th th = new Th(); th.setName("任务线程" + i); th.start(); threads.add(th); } } public static void main(String[] args) { while(!tasks.isEmpty()){ if(!threads.isEmpty()){ String task = tasks.remove(0); Th t = threads.remove(0); t.setTask(task); } } }}class Th extends Thread{ private String task; public void run(){ while(!ThreadRun.tasks.isEmpty()){ if(task != null){ System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......"); task = null; ThreadRun.threads.add(this); } } if(task != null){ System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......"); task = null; ThreadRun.threads.add(this); } } public void setTask(String task){ this.task = task; }} netbeans使用问题!急求帮助!! Jbulider在window7下花屏如何解决 请大虾帮帮忙,我这段程序为什么在eclipse下不能显示图片呢? 关于Applet与servlet通讯问题,高手请进,在线等! 问一个比较初级的问题:如何运行java程序 谁有开发SPIDER的com.heaton.bot包发给我下 谢谢 请求解决问题!关于String的排序问题! 关于线程内存分配 如何把一个Image对象存储为一张图片,不是一个文件的copy! 求教各位大神关于Java g2d绘图的问题 编译错误和运行错误,有什么区别? 求查找一段文字中的个数的代码!小弟刚学不久
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 6, 3,TimeUnit.SECONDS,
//缓冲队列为3
new ArrayBlockingQueue<Runnable>(3),
//抛弃旧的任务
new ThreadPoolExecutor.DiscardOldestPolicy());
threadPool.execute(Task task);
Task类必须实现Runnable接口。
如果共享数据 需要考虑同步问题
package com.study.thread;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class ThreadRun { public static List<String> tasks = Collections.synchronizedList(new ArrayList<String>());
public static List<Th> threads = Collections.synchronizedList(new ArrayList<Th>());
static{
for(int i = 0; i < 100; i++){
tasks.add("任务" + i);
}
for(int i = 0; i < 10; i++){
Th th = new Th();
th.setName("任务线程" + i);
th.start();
threads.add(th);
}
}
public static void main(String[] args) {
while(!tasks.isEmpty()){
String task = tasks.remove(0);
if(!threads.isEmpty()){
Th t = threads.remove(0);
t.setTask(task);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Th extends Thread{
private String task;
public void run(){
while(!ThreadRun.tasks.isEmpty()){
if(task != null){
System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......");
task = null;
ThreadRun.threads.add(this);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setTask(String task){
this.task = task;
}
}
package com.study.thread;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class ThreadRun { public static List<String> tasks = Collections.synchronizedList(new ArrayList<String>());
public static List<Th> threads = Collections.synchronizedList(new ArrayList<Th>());
static{
for(int i = 0; i < 100; i++){
tasks.add("任务" + i);
}
for(int i = 0; i < 10; i++){
Th th = new Th();
th.setName("任务线程" + i);
th.start();
threads.add(th);
}
}
public static void main(String[] args) {
while(!tasks.isEmpty()){
if(!threads.isEmpty()){
String task = tasks.remove(0);
Th t = threads.remove(0);
t.setTask(task);
}
}
}
}class Th extends Thread{
private String task;
public void run(){
while(!ThreadRun.tasks.isEmpty()){
if(task != null){
System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......");
task = null;
ThreadRun.threads.add(this);
}
}
if(task != null){
System.out.println("任务 : [" + task + "]被[" + this.getName() + "]执行......");
task = null;
ThreadRun.threads.add(this);
}
}
public void setTask(String task){
this.task = task;
}
}