一.继承Thread类
public class Thread1 extends Thread {
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println(this.getName()+"正在运行"+i);
}
}
public static void main(String[] args){
Thread1 t1 = new Thread1();
Thread1 t2 = new Thread1();
t1.start();
t2.start();
}
}
运行结果:
Thread-0正在运行0
Thread-1正在运行0
Thread-1正在运行1
Thread-0正在运行1
Thread-0正在运行2
Thread-1正在运行2
Thread-1正在运行3
Thread-1正在运行4
Thread-0正在运行3
Thread-0正在运行4
需要注意,这里的运行结果是随机的,原因是需要调用线程实例.start(),但这只是让线程处于就绪状态,具体什么时候执行,按什么顺序执行都是由操作系统自身控制的。
二.实现Runnable接口
public class Demo implements Runnable {
private String name;
public Demo(String name) {
super();
this.name = name;
}
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println(this.name+"在运行"+i);
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Demo("thread1"));
t1.start();
Thread t2 = new Thread(new Demo("thread2"));
t2.start();
}
}
运行结果:
thread1在运行0
thread2在运行0
thread1在运行1
thread2在运行1
thread1在运行2
thread1在运行3
thread1在运行4
thread2在运行2
thread2在运行3
thread2在运行4
这里的运行结果也是随机的,原因与上文叙述一样。实现Runnable比继承extends的方法更好,因为它解耦更好,它把线程与县城任务分离,且符合Java面向接口编程的思想。
三.匿名内部类的方式
方式1:
public class Thread3 {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println(this.getName()+"正在运行"+i);
}
}
}.start();
for(int i=0;i<5;i++){
System.out.println("主线程正在运行");
}
}
}
方式2:
public class Thread3 {
public static void main(String[] args) {
new Thread(new Runnable(){
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println("线程1在运行"+i);
}
}
}).start();
for(int i=0;i<5;i++){
System.out.println("主线程正在运行");
}
}
}
匿名内部类的方式更方便,但需要注意用匿名内部类创建的线程只能使用一次,所以用的概率并不大。
四.实现Callable(带返回值的线程)
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Demo4 implements Callable<Integer>{
private String name;
public Demo4(String name){
this.name=name;
}
@Override
public Integer call() throws Exception {
int i;
for(i=0;i<5;i++){
System.out.println(this.name+"正在运行"+i);
}
return i;
}
public static void main(String[] args) {
Demo4 d = new Demo4("线程1");
FutureTask<Integer> task = new FutureTask<>(d);
Thread t =new Thread(task);
t.start();
Demo4 d2 = new Demo4("线程2");
FutureTask<Integer> task2 = new FutureTask<>(d2);
Thread t2 =new Thread(task2);
t2.start();
try {
Integer a = task.get();
System.out.println(a);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
线程2正在运行0
线程1正在运行0
线程2正在运行1
线程1正在运行1
线程1正在运行2
线程2正在运行2
线程2正在运行3
线程2正在运行4
线程1正在运行3
线程1正在运行4
5
五.线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo5 {
public static void main(String[] args){
ExecutorService threadPool = Executors.newFixedThreadPool(3);
for(int i=0;i<3;i++){
threadPool.execute(new Runnable(){
@Override
public void run() {
for(int i=0;i<3;i++){
System.out.println("线程"+i+"正在运行"+i);
}
}
});
}
threadPool.shutdown();
}
}
运行结果:
线程0正在运行0
线程0正在运行0
线程1正在运行1
线程2正在运行2
线程0正在运行0
线程1正在运行1
线程2正在运行2
线程1正在运行1
线程2正在运行2