静态代理
- 真实对象和代理对象都要实现同一个接口
- 代理对象要代理真实角色
- 好处:代理对象可以做很多真实对象做不了的事情;真实对象专注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
new Thread(() -> System.out.println("我爱你")).start();
new WeddingCompany(new You()).HappyMarry();
}
}
interface Marry {
void HappyMarry();
}
//真实角色,去结婚
class You implements Marry {
@Override
public void HappyMarry() {
System.out.println("我结婚啦!");
}
}
//代理角色,帮助你结婚
class 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("结婚之后");
}
}
Lambda表达式
- 避免匿名内部类定义过多
- 实质属于函数式编程概念
//推导lambda表达式
public class TestLambda {
//2.静态内部类
static class Like2 implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda2");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
like = new Like2();
like.lambda();
//3.局部内部类
class Like3 implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda3");
}
}
like = new Like3();
like.lambda();
//4.匿名内部类,没有类的名称,必须借助接口或者父类
like = new ILike() {
@Override
public void lambda() {
System.out.println("I like lambda4");
}
};
like.lambda();
//5.lambda简化
like = () -> System.out.println("I like lambda5");
like.lambda();
}
}
//定义一个函数式接口
interface ILike {
void lambda();
}
//1.实现类
class Like implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda");
}
}
总结:
1.lambda表达式只能有一行代码的情况下才能简化为一行,如果有多行则用代码块包裹。
2.必须是函数式接口,一个接口只能有一个方法。
3.多个参数也可以去掉参数类型,但必须都去掉,加上括号。
线程方法
停止线程
建议使用一个标志位进行终止变量,当flag=false,则终止线程运行。
//测试stop
//1.建议线程正常停止-->利用次数,不建议死循环
//2.建议使用标志位-->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法
public class TestStop implements Runnable {
//1.设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag) {
System.out.println("线程正在运行-->" + i++);
}
}
//2.设置一个公开方法停止线程,转换标志位
public void stop() {
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main" + i);
//调用stop方法切换标志位,让线程停止
if (i == 900) {
testStop.stop();
System.out.println("线程停止");
}
}
}
}
线程休眠
- sleep指定当前线程阻塞的毫秒数
- sleep存在异常InterruptedException
- sleep时间达到后线程进入就绪状态
- sleep可以模拟网络延时,倒计时等
- 每一个对象都有一个锁,sleep不会释放锁
public class TestSleep implements Runnable {
private boolean flag = true;
@Override
public void run() {
int i = 10;
System.out.println("倒计时10s");
while (flag) {
System.out.println(i-- + "s");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == 0) {
System.out.println("开炮!!!");
stop();
}
}
}
public void stop() {
this.flag = false;
}
public static void main(String[] args) {
TestSleep testSleep = new TestSleep();
new Thread(testSleep).start();
}
}
线程礼让
- 礼让线程,让当前正在执行的线程暂停,但不阻塞
- 让线程从运行状态转为就绪状态
- 让cpu重新调度,礼让不一定成功!看CPU心情
//测试礼让线程,看CPU心情
public class TestYeild {
public static void main(String[] args) {
MyYeild myYeild = new MyYeild();
new Thread(myYeild, "A").start();
new Thread(myYeild, "B").start();
}
}
class MyYeild implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->运行");
Thread.yield();
System.out.println(Thread.currentThread().getName() + "-->停止");
}
}
线程合并(插队)
//测试Join方法,想象为插队
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程vip来了" + i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主线程
for (int i = 0; i < 1000; i++) {
if (i == 200) {
thread.join();//插队
}
System.out.println("main" + i);
}
}
}
线程状态观测
- 线程五个状态:新生,就绪,运行,阻塞,死亡
//观察测试线程状态
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("//////////");
});
//观察状态
Thread.State state = thread.getState();
System.out.println(state);//New
//观察启动后
thread.start();
state = thread.getState();
System.out.println(state);//Run
while (state != Thread.State.TERMINATED) {//只要线程不中止,就一直输出状态
Thread.sleep(1000);
state = thread.getState();
System.out.println(state);
}
}
}
线程优先级
- Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行
- 现成的优先级用数字表示,范围从1~10,设置不在范围内的值会报错
-
- Thread.MINRIORITY = 1;
-
- Thread.MAX_RITY = 1;
-
- Thread.NORN_RITY = 1;
- 使用以下方式改变或获取优先级
- getPriority(),setPriority(int xxx)
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread thread1 = new Thread(myPriority);
Thread thread2 = new Thread(myPriority);
Thread thread3 = new Thread(myPriority);
Thread thread4 = new Thread(myPriority);
Thread thread5 = new Thread(myPriority);
Thread thread6 = new Thread(myPriority);
//先设置优先级,再启动
thread1.start();
thread2.setPriority(1);
thread2.start();
thread3.setPriority(4);
thread3.start();
thread4.setPriority(10);
thread4.start();
thread5.setPriority(-5);
thread5.start();
thread6.setPriority(15);
thread6.start();
}
}
class MyPriority implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());
}
}
守护(daemon)线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 如:后台记录操作日志,监控内存,垃圾回收等待
//测试守护线程
//上帝守护你
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread t1 = new Thread(god);
t1.setDaemon(true);//默认为false用户线程
t1.start();
Thread t2 = new Thread(you);
t2.start();
Thread.State state = t2.getState();
if (state == Thread.State.TERMINATED) {
god.stop();
}
}
}
//上帝
class God implements Runnable {
private boolean flag = true;
@Override
public void run() {
while (flag) {
System.out.println("上帝保佑你");
}
}
public void stop() {
this.flag = false;
}
}
//你
class You implements Runnable {
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("我活了" + i + "天");
}
System.out.println("==GoodBye World==");
}
}