第一种:无需继承类与实现接口,最简单实现
public static void main(String[] args) {
//第一个线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i=1;i<=1000;i++) {
System.out.println("线程1"+i);
}
}
}).start();
//第二个线程
new Thread(new Runnable() {
@Override
public void run() {
for (int i=1;i<=1000;i++) {
System.out.println("线程2"+i);
}
}
}).start();
}
第二种:实现Runnable接口
public class runner implements Runnable {
private List<Integer> ii;
//重写构造方法,传参用
public runner(List<Integer> i){
this.ii = i;
}
@Override
public void run() {
for (Integer i:ii){
System.out.print(Thread.currentThread().getName()+":"+i);
}
System.out.println();
}
}
public class test {
public static void main(String[] args) {
//拆分list执行线程
List<Integer> list = new ArrayList();
for (int i =0;i <10000;i++){
list.add(i);
}
int i= list.size();
List<Integer> list1 = list.subList(0,i/3);
List<Integer> list2 = list.subList(i/3,(i/3)*2);
List<Integer> list3 = list.subList((i/3)*2,i);
runner runner1 = new runner(list1);
runner runner2 = new runner(list2);
runner runner3 = new runner(list3);
new Thread(runner1,"线程1").start();
new Thread(runner2,"线程2").start();
new Thread(runner3,"线程3").start();
}
}
第三种:继承Thread类
public class ThreadDemo extends Thread{
//重写构造方法,可不写,可放“参数”
private String str;
public ThreadDemo(String str){
this.str = str;
}
public void run(){
for(int i = 0;i<=1000;i++){
System.out.println(Thread.currentThread().getName()+str+i);
}
}
}
public class test{
public static void main(String[] args){
ThreadDemo threadDemo = new ThreadDemo("小明");
threadDemo.setName("线程名");
threadDemo.start();
//拆分list多线程执行参考第二种方法
}
}
第四种:通过Callable和FutureTask创建线程
a:创建Callable接口的实现类 ,并实现Call方法
b:创建Callable实现类的实现,使用FutureTask类包装Callable对象,该FutureTask对象封装了Callable对象的Call方法的返回值
c:使用FutureTask对象作为Thread对象的target创建并启动线程
d:调用FutureTask对象的get()来获取子线程执行结束的返回值
public class CallableDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Callable<Object> oneCallable = new Tickets<Object>();
FutureTask<Object> oneTask = new FutureTask<Object>(oneCallable);
Thread t = new Thread(oneTask);
System.out.println(Thread.currentThread().getName());
t.start();
}
}
class Tickets<Object> implements Callable<Object>{
//重写call方法
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"-->我是通过实现Callable接口实现的线程");
return null;
}
}
第五种:通过线程池创建线程
public class ThreadDemo{
private static int POOL_NUM = 10; //线程池数量
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i<POOL_NUM; i++)
{
RunnableThread thread = new RunnableThread();
//Thread.sleep(1000);
executorService.execute(thread);
}
//关闭线程池
executorService.shutdown();
}
}
class RunnableThread implements Runnable
{
@Override
public void run()
{
System.out.println("通过线程池方式创建的线程:" + Thread.currentThread().getName() + " ");
}
}