第一种:继承Thread
//继承方式创建多线程
class MyThread extends Thread{
@Override
//重写run()
public void run() {
for (int i = 0; i <100 ; i++) {
System.out.println(getName() + i);
}
}
}
public class ThreadExtends{
public static void main(String[] args) {
MyThread myThread=new MyThread();
myThread.start();
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName() + "main----------");
}
}
}
第二种:实现Runnable
class MyThreadRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class ThreadRunnable {
public static void main(String[] args) {
MyThreadRunnable myThreadRunnable=new MyThreadRunnable();
Thread t=new Thread(myThreadRunnable);
t.start();
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName() + "-----------main");
}
}
}
相对于继承Thread,实现Runnable避免了单继承的局限性
jdk5.0后新增
第三种:实现Callable
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class MyThreadCallable implements Callable { //可添加泛型
private int sum=0;
@Override
public Object call() throws Exception {
for (int i = 1; i <=100 ; i++) {
System.out.println(Thread.currentThread().getName() + i);
sum += i;
}
return sum;
}
}
public class ThreadCallable {
public static void main(String[] args) {
MyThreadCallable myThreadCallable=new MyThreadCallable();
FutureTask futureTask=new FutureTask(myThreadCallable);
new Thread(futureTask).start();
try {
//获取返回值
Object sum=futureTask.get();
System.out.println("和为:" + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
相对于Runnable,Callable具有以下几个不同点:
1)、Callable中的Call()具有返回值
2)、Callable中的Call()可以向外抛出异常,捕获异常信息
3)、Callable是可以使用泛型的
第四种:使用线程池
import java.util.concurrent.*;
//使用线程池创建多线程
class MyThreadPool implements Runnable{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
class MyThreadPool1 implements Runnable{
@Override
public void run() {
for (int i = 0; i <100 ; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
class MyThreadPool2 implements Callable{
private int sum=0;
@Override
public Object call() throws Exception {
for (int i = 1; i <=100 ; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
sum += i;
}
return sum;
}
}
public class ThreadPool {
public static void main(String[] args) {
//创建线程池
ExecutorService service= Executors.newFixedThreadPool(10);
//设置线程池属性
// ThreadPoolExecutor threadPoolExecutor=(ThreadPoolExecutor)service;
// threadPoolExecutor.setCorePoolSize();
// threadPoolExecutor.setKeepAliveTime();
//...
MyThreadPool mythreadpool=new MyThreadPool();
MyThreadPool1 mythreadpool1=new MyThreadPool1();
// MyThreadPool2 mythreadpool2=new MyThreadPool2();
//适用于实现Runnable接口
service.execute(mythreadpool);
service.execute(mythreadpool1);
//适用于实现Callable接口
// Future sum= service.submit(mythreadpool2); //可以接收Call()的返回值
// try {
// System.out.println("和为:" + sum.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
//关闭线程池
service.shutdown();
}
}
优点:使用线程池创建线程减少了创建线程的时间,降低了资源消耗,便于管理。
以上就是创建多线程的方式,不喜勿喷,有不足之处可以指出。