JAVA中创建多线程主要有以下三种方式:
方式一:继承Thread类,重写run()方法
/**
* 模拟龟兔赛跑
*/
public class Rabbit extends Thread{
@Override
public void run() {
for(int i=0;i<100;++i) {
System.out.println("兔子跑了"+i+"步");
}
}
public static void main(String[] args) {
//创建子类对象
Rabbit rab = new Rabbit();
Tortle tor = new Tortle();
//调用start方法,调用此方法后将线程加入到线程组中,等待CPU调度
rab.start();
tor.start();
for(int i = 0;i<100;++i) {
System.out.println("main跑了"+i+"步");
}
}
}
class Tortle extends Thread{
@Override
public void run() {
for(int i=0;i<100;++i) {
System.out.println("乌龟跑了"+i+"步");
}
}
}
方式二:实现Runnable接口,重写run()方法。基于静态代理模式思想
/**
* 使用Runnable创建线程
* 1、类实现Runnable接口+重写run() ----》真实角色
* 2、启动多线程: 使用静态代理
* 1) 创建真实角色
* 2)创建代理角色+真实角色引用
* 3)调用.start() 启动线程
* @author Administrator
*/
public class RabbitImRunable implements Runnable{
@Override
public void run() {
for(int i = 0;i<100;++i) {
System.out.println("兔子跑"+i+"步");
}
}
public static void main(String[] args) {
RabbitImRunable rab = new RabbitImRunable();
Thread th1 = new Thread(rab);
TortleImRunable tor = new TortleImRunable();
Thread th2 = new Thread(tor);
th1.start();
th2.start();
for(int i=0;i<100;++i) {
System.out.println("Main-->"+i+"步");
}
}
}
class TortleImRunable implements Runnable{
@Override
public void run() {
for(int i = 0;i<100;++i) {
System.out.println("乌龟跑"+i+"步");
}
}
}
方式三:实现Callable接口重写call方法(此方式创建多线程的优势是可以获取返回值,能抛出异常,缺点是较为繁琐)
/**
*
* 1、实现Callable接口,重写call方法
* 2、借助执行调度服务ExecutorService创建线程: ExecutorService ser = Executors.newFixedThreadPool(线程个数);
* 3、获取Future对象: Future<Integer> result = ser.submit(实现Callable接口类的对象);
* 4、通过Future对象获取值 : result.get();
* 5、停止服务: ser.shutdownNow();
* @author Administrator
*
*/
public class Call {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//创建线程
ExecutorService ser = Executors.newFixedThreadPool(2);
Race tortoise = new Race("乌龟",1000); //实现类对象
Race rabbit = new Race("兔子",500); //实现类对象
//获取Future对象
Future<Integer> result1 = ser.submit(tortoise);
Future<Integer> result2 = ser.submit(rabbit);
Thread.sleep(2000);//跑两秒
tortoise.setFlag(false); //停止线程体循环
rabbit.setFlag(false);
//获取值
int num1 = result1.get();
int num2 = result2.get();
System.out.println("乌龟跑----》"+num1+"步");
System.out.println("兔子跑---》"+num2+"步");
//停止服务
ser.shutdownNow();
}
}
class Race implements Callable<Integer>{
private String name; //对象名称
private long time; //延迟时间
private boolean flag=true;
private int step = 0; //步数
public Race(String name) {
super();
this.name = name;
}
public Race(String name, long time) {
super();
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
@Override
public Integer call() throws Exception {
while(flag) {
Thread.sleep(time); //延时
++step;
}
return step;
}
}