我有一个List集合,里面放了10000条数据,从1 -- 10000 int类型数字,现在我想启动10个线程,第一个线程打印1 -- 1000 ,第二个线程打印1001 -- 2000,一次类推。10个线程都是同时启动的,所以打印的数据应该是凌乱的,
可能是1、2、1001、2001、4001、3001、5001......
我对线程不是很熟悉,所以这样的功能不知道怎么做,希望大家知道下……
public class MYThread extends Thread {
private int beg;
private int end;
public MYThread (int beg,int end){
this.beg=beg;
this.end=end; }
@Override
public void run() {
this.gramDis();
}
public void gramDis(){
//写具体打印代码
}
}
//调用
public class StartThread(){
public static void main(String[] args) {
MYThread th1=new MYThread (1,1000);
MYThread th2=new MYThread (1,1000);
th1.start();
th2.start();
}
}
既然使用多线程那你的目的肯定是都打印,而不是打印顺序。多线程打印无序。你可以讲list的size取出来,如你所讲大概10000个,你开启10个线程。那么你可以知道每个线程可以分得1000个。这样的话你就可以确认 第一个线程分得list[0]到list[999],第二个取得list[1000]到list[1999]以此类推。
thread.join()
import java.util.ArrayList;
import java.util.List;
public class TestThread {
public static void main(String[] args){
List list = new ArrayList();
for(int i=0;i<10000;i++){
list.add(i);
}
for(int i=0;i<10;i++){
new MyThread(i*1000,list).start();
}
}
}
class MyThread extends Thread {
private int start;
private List list;
public MyThread(int start,List list){
this.start=start;
this.list=list;
}
public void run(){
for(int i=start;i<start+1000;i++){
System.out.println(list.get(i));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
同意楼上!
多线程就是为了能够同时执行多个不同操作
如果要等一个线程执行完打印任务之后再执行另一个打印线程,
那就完全没必要使用多线程
[code="java"]import java.util.ArrayList;
import java.util.List;
/**
create time 2013-9-25
*/
public class TestJoin {
private static final int thousand = 1000;
private static final List nums = new ArrayList(thousand * 10);
static{
for(int i=1; i<=thousand * 10; i++){
nums.add(i);
}
}
public static void main(String[] args) {
PrintThread t1,t2,t3,t4,t5,t6,t7,t8,t9,t10 = null;
t1 = new PrintThread(null, 1);
t2 = new PrintThread(t1, 2);
t3 = new PrintThread(t2, 3);
t4 = new PrintThread(t3, 4);
t5 = new PrintThread(t4, 5);
t6 = new PrintThread(t5, 6);
t7 = new PrintThread(t6, 7);
t8 = new PrintThread(t7, 8);
t9 = new PrintThread(t8, 9);
t10 = new PrintThread(t9, 10);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
t9.start();
t10.start();
}
static class PrintThread extends Thread{
private Thread preThread;
private int position;
public PrintThread(Thread preThread, int position) {
this.preThread = preThread;
this.position = position;
}
@Override
public void run(){
try {
if(preThread != null){
preThread.join();
}
print();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void print(){
System.out.println("thread[" + position + "] print:");
int index = position -1;
for(int i=index*thousand; i<index*thousand+thousand; i++){
System.out.println(nums.get(i));
}
System.out.println("==========================");
}
}
}[/code]
使用Thread.join实现
join用法参考 http://zy19982004.iteye.com/blog/1626916
如果没有打印顺序要求的话,那么在初始化每个Thread对象的时候指定其打印list的范围,比如1 - 1000,然后根据这个范围循环读取list进行打印。
无聊~蛋疼
无聊得超级蛋疼
可以用Executor来管理你的线程,其中的newFixedThreadPool()方法可以用来开启固定数量的线程。也可以用newSingleThreadExecutor()来一次执行单一线程的功能。