线程脉络详解
一、线程的创建(三种方式):
继承Thread类、实现Runnable接口、实现Callable接口
1、Thread
1、要创建类继承Thread,
2、重写run()方法
3、调用start()方法启动线程
//1、创建MyThread继承Thread
public class MyThread extends Thread{
//2、重写run()方法
@Override
public void run(){
for(int i = 0; i<10; i++){
System.out.printfln("我在看线程"+i);
}
}
public static void main(String[] args){
MyThread t = new MyThread();
// t.run();
// 使用start()方法启动新的线程
t.start();
//为了看清线程添加了主线程循环
for (int i = 0; i < 2000; i++) {
System.out.println("我在看代码 "+i);
}
}
}
注:这里可以通过run()和start()方法测试线程调用的顺序
这里说出我的结论:
调用run()方法的时候没有启动新的线程,还是顺序执行,和调用普通方法一样。
调用start()方法,可以看出启动了新的线程,和main()主线程一起执行。
2、Runnable
1、实现Runnable接口
2、重写run()方法
3、创建实现类 ,创建Thread,把实现类放入线程,调用start()方法。
//1、创建Runnable实现类
public class MyRunnable implements Runnable{
//2、重写run()方法
@Override
public void run(){
for(int i = 0; i<10; i++){
System.out.printfln("我在看线程"+i);
}
}
public static void main(String[] args){
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
//可以设定名称 Thread t = new Thread(r,"线程名称");
}
}
3、Callable
1、实现Callable接口
2、重写call()方法
3、创建实现类,创建执行服务,通过submit()方法提交任务,通过get()方法获取返回值,通过shutdown()/shutdownNow()方法结束执行
//实现Callable接口
public class MyCallable implements Callable{
private String a;
public MyCallable(String a){
this.a = a;
}
//重写call()方法
@Override
public String call(){
Method m = new Method();
String result = m.f(a);
return result;
}
public static void main(String[] args){
//创建实现类实例
Callable c1 = new MyCallable("1");
Callable c2 = new MyCallable("2");
Callable c3 = new MyCallable("3");
//创建执行服务
ExecutorService exec = Executors.newFixedThreadPool(3);
//提交执行
Future<String> f1 = exec.submit(c1);
Future<String> f2 = exec.submit(c2);
Future<String> f3 = exec.submit(c3);
//获取结果
String rs1 = f1.get();
String rs2 = f2.get();
String rs3 = f3.get();
//关闭服务
exec.shutdownNow();
}
}
class Method{
public String f(String a){
System.out.println(a);
return a;
}
}
二、线程的静态代理模式
1、实现方法:
(1)、真实对象和代理对象都要实现同一个接口
(2)、 代理对象要代理真实角色,即代理对象要获取真实角色
2、理念
代理对象做周边业务,打辅助,真实对象做主业务,打C位。
3、代码示例
public class StaticProxyLOL{
public static void main(String[] args){
ADC adc = new ADC();
FZ fz = new FZ(adc);
fz.action();
}
}
//接口
interface LOL{
public void action();
}
//代理对象
class FZ implements LOL{
private LOL lol;
publicpublic class StaticProxyLOL {
public static void main(String[] args){
ADC adc = new ADC();
FZ fz = new FZ(adc);
fz.action();
}
}
//接口
interface LOL{
public void action();
}
//代理对象
class FZ implements LOL{
private LOL lol;
public FZ(LOL lol){
this.lol = lol;
}
@Override
public void action(){
before();
this.lol.action();
after();
}
private void before(){
System.out.println("我是辅助要垫刀");
System.out.println("我是辅助要开团");
}
private void after(){
System.out.println("我是辅助要给adc加状态");
}
}
//真实角色
class ADC implements LOL{
@Override
public void action(){
System.out.println("我adc要补兵");
System.out.println("我adc抢人头");
}
} FZ(LOL lol){
this.lol = lol;
}
@Override
public void action(){
before();
this.lol.action();
after();
}
private void before(){
System.out.println("我要垫刀");
System.out.println("我要开团");
}
private void after(){
System.out.println("我要给adc加状态");
}
}
//真实角色
class ADC implements LOL{
@Override
public void action(){
System.out.println("我要补兵");
System.out.println("我抢人头");
}
}