静态代理:
package demo2;
//静态代理模式总结:
//真实对象和代理对象都要事先同一个接口
//代理对象要代理真实角色
//好处,代理对象可以做到真实对象做不了的事情
//真实对象可以专注做自己的事情
interface Marry{
//人间四大喜事
//久旱逢甘霖
//他乡遇故知
//洞房花烛夜
//金榜题名时
void HappyMarry();
}
//真实角色,你去结婚
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("舍友要结婚了,超开心");
}
}
//代理对象,帮助你结婚
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target)
{
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();//这就是真实对象
after();
}
private void before() {
System.out.println("结婚之前布置现场");
}
private void after() {
System.out.println("结婚之后收尾款");
}
}
public class StaticProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(()->System.out.println("我和你") ).start();
new WeddingCompany(new You()).HappyMarry();
You you = new You();
/*you.HappyMarry();*/
WeddingCompany w = new WeddingCompany(you);
w.HappyMarry();
}
}
运行结果:
Lambda表达式
任何接口,如果zhi只包含唯一一个抽象方法,那么他就是一个函数式接口。(我们就可以用lambda表达式了)
package demo2;
/*
* 推导lambda表达式
*/
//1.定义一个函数式接口
interface ILike{
void lambda();
}
//2.实现类
class Like implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda");
}
}
public class TestLambda1 {
//3.静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda2");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ILike like = new Like();
like .lambda();
ILike like2 = new Like2();
like2 .lambda();
//4.局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("i like lambda3");
}
}
ILike like3 = new Like3();
like3 .lambda();
//匿名内部类,没有类的名称必须借助接口或者弗雷
like =new ILike() {
@Override
public void lambda() {
System.out.println("i like lambda4");
}
};
like .lambda();
//用lambda简化
like = () ->{
System.out.println("i like lambda5");
};
like.lambda();
}
}
运行结果:
线程停止
package demo2;
//测试stop
//1.建议线程正常停止,利用次数,不建议死循环
//2.建议使用标志位,设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法
public class TestStop implements Runnable{
//设置一个标识位
private boolean flag = true;
@Override
public void run()
{
int i=0;
while(flag)
{
System.out.println("run...Thread"+i++);
}
}
//2.设置一个公开的方法停止线程,转换标志位
public void stop()
{
this.flag = false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestStop t = new TestStop();
new Thread(t).start();
for(int i=0;i<1000;i++)
{
System.out.println("Main"+i);
if(i==900)
{
//调用我们自己的stop方法,让线程停止
t.stop();
System.out.println("线程停止了");
}
}
}
}
线程休眠:
package demo2;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep {
public static void main(String[] args) {
// TODO Auto-generated method stub
//打印系统时间
Date startTime = new Date(System.currentTimeMillis());//获取系统当前时间
int num = 15;
while(true)
{
if(num>0)
{
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());//更新时间
}catch(InterruptedException e){
e.printStackTrace();
}
}
else
{
break;
}
num--;
}
System.out.println("stop");
}
//模拟倒计时
public static void tenDown() throws InterruptedException{
int num = 10;
while(true)
{
Thread.sleep(1000);
System.out.println(num--);
if(num<=0)
break;
}
}
}
线程礼让
package demo2;
//测试礼让线程
//礼让不一定成功,看cpu心情
public class testYield {
public static void main(String[] args) {
// TODO Auto-generated method stub
myYield m = new myYield();
new Thread(m,"a").start();
new Thread(m,"b").start();
}
}
class myYield implements Runnable{
@Override
public void run()
{
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();//线程开始礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
运行结果:
线程强制执行
package demo2;
//测试join方法//想象为插队
public class testjoin implements Runnable{
@Override
public void run() {
for(int i=0;i<300;i++)
{
System.out.println("线程vip来了"+i);
}
}
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
testjoin t =new testjoin();
Thread thread = new Thread(t);
thread.start();
//主线程
for(int i=0;i<1000;i++)
{
if(i==200)
{
thread.join();//插队
}
System.out.println("main"+i);
}
}
}
运行结果: