java多线程处理任务

阿宝洁猪 2012-12-20 10:43:43
现在有一堆任务,要求10个线程同时执行,每个任务只能被一个线程执行,直到把所有任务执行完毕。怎么写啊。哪个大哥帮写一下
...全文
447 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-12-25
  • 打赏
  • 举报
回复

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;
	}
}

阿宝洁猪 2012-12-24
  • 打赏
  • 举报
回复
意思是6楼那个意思,但是能不能不休息啊,每次都休息一秒,效率太低下了。
Candylibin 2012-12-23
  • 打赏
  • 举报
回复
引用 6 楼 abstruct 的回复:
你的线程只要不结束他就可以了,写了个例子,希望对你有帮助 Java code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657package com.study.thread; import java.util……
顶光哥哈哈
yktd26 2012-12-21
  • 打赏
  • 举报
回复
concurrent 包里有不少executor可以实现你的要求
迹- 2012-12-21
  • 打赏
  • 举报
回复
创建一个线程池,然后执行任务
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 6, 3,TimeUnit.SECONDS, 
				//缓冲队列为3
				new ArrayBlockingQueue<Runnable>(3),
				//抛弃旧的任务
				new ThreadPoolExecutor.DiscardOldestPolicy());
threadPool.execute(Task task);
Task类必须实现Runnable接口。 如果共享数据 需要考虑同步问题
安特矮油 2012-12-21
  • 打赏
  • 举报
回复
你的线程只要不结束他就可以了,写了个例子,希望对你有帮助
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;
	}
}
面向玄学编程 2012-12-20
  • 打赏
  • 举报
回复
线程池啊。同上,如果你要同步数据还要加锁
傲雪kimi 2012-12-20
  • 打赏
  • 举报
回复
这10个线程涉及到共享数据吗

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧