Java学习笔记(八)――java多线程(三)
8 public void sellClothes() {
9 t.sellClothes();
10 System.out.println("我是中间商,我买的是制造商的衣服");
11 System.out.println("我是中间商,我还提供对裤子不合适的进行裁剪服务");
12 }
13 }
复制代码
SellClothes.java
1 //实际被代理角色
2 public class Manufacturer implements SellClothes{
3 public void sellClothes() {
4 System.out.println("我是制造商,我提供批发衣服服务");
5 }
6 }
Test.java
复制代码
1 public class Test {
2 public static void main(String[] args) {
3 Manufacturer t = new Manufacturer();
4 Middleman sellclothes = new Middleman(t);
5 sellclothes.sellClothes();
6 }
7 }
复制代码
运行结果:
1 我是制造商,我提供批发衣服服务
2 我是中间商,我买的是制造商的衣服
3 我是中间商,我还提供对裤子不合适的进行裁剪服务
得出结论:
抽象主题角色:Runnable,提供run方法
代理主题角色:Thread类,提供run方法
实际被代理角色:ThreadTest2,也实现了run 方法
这就是代理主题模式,我希望我讲清楚了,哈哈
3. native关键字
在看到start()实现方法的时候,看到了如下一段代码:
复制代码
1 public synchronized void start() {
2 /**
3 * This method is not invoked for the main method thread or "system"
4 * group threads created/set up by the VM. Any new functionality added
5 * to this method in the future may have to also be added to the VM.
6 *
7 * A zero status value corresponds to state "NEW".
8 */
9 if (threadStatus != 0)
10 throw new IllegalThreadStateException();
11 group.add(this);
12 start0();
13 if (stopBeforeStart) {
14 stop0(throwableFromStop);
15 }
16 }