-
开心每一天1111
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]以此类推。
-
宝慕林4294392
thread.join()
-
BIG阳
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();
}
}
}}
-
一只萌萌小番薯
同意楼上!多线程就是为了能够同时执行多个不同操作如果要等一个线程执行完打印任务之后再执行另一个打印线程,那就完全没必要使用多线程
-
不负相思意
如果没有打印顺序要求的话,那么在初始化每个Thread对象的时候指定其打印list的范围,比如1 - 1000,然后根据这个范围循环读取list进行打印。
-
翻过高山走不出你
可以用Executor来管理你的线程,其中的newFixedThreadPool()方法可以用来开启固定数量的线程。也可以用newSingleThreadExecutor()来一次执行单一线程的功能。