1. 继承Thread类
/**
* 通过集成Thread类实现多线程
* 重写Thread类的run(),run方法内的为线程体,该线程实现的功能写在run里面
* 启动线程使用对象。start()
* @author Administrator
*
*/
public class Rabbit extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("兔子跑了"+i+"步");
}
}
}
class Tortoise extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("乌龟跑了"+i+"步");
}
}
}
public class RabbitApp {
public static void main(String[] args) {
Rabbit r = new Rabbit();
Tortoise tor = new Tortoise();
r.start();//启动线程1
tor.start();//启动线程2
}
}
2. 实现Runnable接口
1)静态代理模式
/**
* 实现Runnable接口实现多线程所使用的
* 静态代理设计模式
* 1.有真实角色
* 2.有代理角色:只有真实角色的引用
* 3.二者实现相同接口
* 在多线程模式中
* 真实角色 ---> 创建的类
* 代理角色 ---> Thread类
* @author Administrator
*
*/
public class StaticProxy {
public static void main(String[] args) {
You you = new You();//真实角色
MarryCompany mc = new MarryCompany(you);//代理角色+真实角色引用
mc.marry();
}
}
//共同接口
interface Marry{
public abstract void marry();
}
//真是角色
class You implements Marry{
@Override
public void marry() {
System.out.println("You and Your Lover Marrying");
}
}
//代理角色
class MarryCompany implements Marry{
private Marry you;
public MarryCompany() {
}
public MarryCompany(Marry you) {
this.you = you;
}
private void before(){
System.out.println("布置现场");
}
private void after(){
System.out.println("整理残局");
}
@Override
public void marry() {
before();
you.marry();
after();
}
}
2)实现
/**
* 使用Runnable接口实现多线程 --->避免Java单继承缺陷 便于共享资源
* 创建类实现Runnable接口 +重写run()方法
* 使用静态代理 启动多线程
* 创建真实角色
* 创建代理角色+真实角色引用 Thread类
* 调用。start()
*
* @author Administrator
*
*/
public class Programmer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("多线程1:"+i);
}
}
}
public class ProgrammerApp {
public static void main(String[] args) {
// 创建真实角色
Programmer pro = new Programmer();
// 创建代理角色+真实角色引用
Thread porxy = new Thread(pro);
// 调用。start()
porxy.start();//线程1
//线程2
for (int i = 0; i < 10; i++) {
System.out.println("多线程2:"+i);
}
}
}
3. 实现Callable接口
/**
* 通过实现Callable接口创建多线程
* 1.可以有返回值
* 2.可以抛出异常
* @author Administrator
*
*/
public class Callable01 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
//创建线程 个数2
ExecutorService ser = Executors.newFixedThreadPool(2);
//实现Callable接口类
Race tortoise = new Race("乌龟",100L);//停止线程循环
Race rabbit = new Race("兔子",50L);
//获取值
Future<Integer> result1 = ser.submit(tortoise);
Future<Integer> result2 = ser.submit(rabbit);
Thread.sleep(1000);
tortoise.setFlag(false);
rabbit.setFlag(false);
int num1 = result1.get();
int num2 = result2.get();
System.out.println("乌龟跑了"+num1);
System.out.println("兔子跑了"+num2);
//停止服务
ser.shutdownNow();
}
}
//使用泛型确定call()的返回值类型
class Race implements Callable<Integer>{
private String name;
private long time;//延时
private int step;
private boolean flag = true;
public Race() {
super();
}
public Race(String name,Long time) {
super();
this.name = name;
this.time = time;
}
@Override
public Integer call() throws Exception {
while(flag){
Thread.sleep(time);
step++;
}
return step;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}