Java-Multi-threading-static proxy mode, lambda expression, thread stop, thread sleep, thread courtesy, thread enforcement
Static proxy mode
package com.zy.thread;
/**
*description: Java--multithreading--static proxy mode
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 21:14
*/
public class StaticProxy {<!-- -->
/*
Static proxy mode:
Both the real object and the proxy object must implement the same interface;
Proxy objects represent real objects.
Proxy objects can do many things that real objects cannot;
Real objects focus on doing their own thing.
*/
public static void main(String[] args) {<!-- -->
new WeddingCompany(new Marrier()). marry();
}
}
interface Marry{<!-- -->
void marry();
}
/**
* @Description real role: married person
* @author zy
* @date 2023-3-21 21:16
*/
class Marrier implements Marry{<!-- -->
@Override
public void marry() {<!-- -->
System.out.println("I'm getting married, I'm so happy!!!");
}
}
/**
* @Description Acting role: wedding company
* @author zy
* @date 2023-3-21 21:17
*/
class WeddingCompany implements Marry{<!-- -->
private Marry marriage;
public WeddingCompany(Marry marry){<!-- -->
this.marrier = marry;
}
@Override
public void marry() {<!-- -->
before();
// real target object
marry. marry();
after();
}
private void before() {<!-- -->
System.out.println("Before the wedding, arrange the scene");
}
private void after() {<!-- -->
System.out.println("After the wedding, the closing payment");
}
}
lambda expressions
package com.zy.thread;
/**
*description: Java--lambda expression--deduce lambda expression
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 21:35
*/
public class StudyLambda {<!-- -->
// 3. Static inner class
static class LambdaTwoImpl implements Lambda{<!-- -->
@Override
public void lambda() {<!-- -->
System.out.println("Learning lambda2 expression λ");
}
}
public static void main(String[] args) {<!-- -->
new LambdaOneImpl(). lambda();
new LambdaTwoImpl(). lambda();
// 4. Local inner class
class LambdaThreeImpl implements Lambda{<!-- -->
@Override
public void lambda() {<!-- -->
System.out.println("Learning lambda3 expression λ");
}
}
new LambdaThreeImpl(). lambda();
// 5. Anonymous inner class: no class name, must use interface or parent class
new Lambda(){<!-- -->
@Override
public void lambda() {<!-- -->
System.out.println("Learning lambda4 expression λ");
}
}.lambda();
// 6. JDK8: Simplified lambda expression
Lambda lambda = ()-> System.out.println("Learning lambda5 expression λ");
lambda. lambda();
}
}
// 1. Define a functional interface: any interface, if it contains only one abstract method, then it is a functional interface
interface Lambda{<!-- -->
void lambda();
}
// 2. Implementation class
class LambdaOneImpl implements Lambda{<!-- -->
@Override
public void lambda() {<!-- -->
System.out.println("Learning lambda expression λ");
}
}
Thread stopped
package com.zy.thread;
/**
*description: Java--Multithreading--Thread stop
* 1. It is recommended that the thread stop normally: the number of utilizations, and an infinite loop is not recommended
* 2. It is recommended to use flags: set a flag
* 3. Do not use outdated methods such as stop or destroy or methods not recommended by JDK
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 22:18
*/
public class StudyThreadStop implements Runnable {<!-- -->
// 1. Set a flag bit
private boolean flag = true;
@Override
public void run() {<!-- -->
int i=0;
while (flag){<!-- -->
i + + ;
System.out.println("thread running---"" + i);
}
}
// 2. Set a public method to stop the thread and switch the flag bit
public void stop(){<!-- -->
this.flag = false;
}
public static void main(String[] args) {<!-- -->
StudyThreadStop threadStop = new StudyThreadStop();
new Thread(threadStop).start();
for (int i = 0; i < 6666278; i ++ ) {<!-- -->
if(i == 6666277){<!-- -->
// Call the custom stop method to switch the flag bit to stop the thread
threadStop. stop();
System.out.println("thread stop---"" + i);
}
}
}
}
Thread sleep
package com.zy.thread;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*description: Java--Multithreading--Thread sleep
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 22:35
*/
public class StudyThreadSleep {<!-- -->
/*
Each object has a lock, sleep will not release the lock
*/
public static void tenDown() throws InterruptedException {<!-- -->
int number = 10;
while (true){<!-- -->
System.out.println(number--);
if (number <= 0){<!-- -->
break;
}
Thread. sleep(1000);
}
}
public static void main(String[] args) {<!-- -->
try {<!-- -->
// countdown
tenDown();
// print the current system time
Date startTime = new Date(System. currentTimeMillis());
while (true){<!-- -->
Thread. sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
// update current time
startTime = new Date(System. currentTimeMillis());
}
} catch (InterruptedException e) {<!-- -->
e.printStackTrace();
}
}
}
Thread courtesy
package com.zy.thread;
/**
*description: Java--Multithreading--Thread politeness
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 22:45
*/
public class StudyThreadYield {<!-- -->
/*
Polite thread, let the currently executing thread suspend, but not block;
Turn the thread from the running state to the ready state;
Let the cpu reschedule, politeness does not necessarily succeed, see cpu scheduling
*/
public static void main(String[] args) {<!-- -->
new Thread(new MyYield(),"a").start();
new Thread(new MyYield(),"b").start();
}
}
class MyYield implements Runnable{<!-- -->
@Override
public void run() {<!-- -->
System.out.println(Thread.currentThread().getName() + "==>thread starts running");
// thread courtesy
Thread. yield();
System.out.println(Thread.currentThread().getName() + "==>thread stop running");
}
}
Thread enforcement
package com.zy.thread;
/**
*description: Java--Multithreading--Thread enforcement
*@program: basic syntax
*@author: zy
*@create: 2023-03-21 22:53
*/
public class StudyThreadJoin implements Runnable {<!-- -->
@Override
public void run() {<!-- -->
for (int i = 0; i < 1000; i ++ ) {<!-- -->
System.out.println("Thread VIP is here" + i);
}
}
public static void main(String[] args) throws InterruptedException {<!-- -->
// start the thread
Thread thread = new Thread(new StudyThreadJoin());
thread. start();
// main thread
for (int i = 0; i < 500; i ++ ) {<!-- -->
if(i == 200){<!-- -->
// Queue jumping: mandatory execution, blocking other threads
thread. join();
}
System.out.println("Main thread running" + i);
}
}
}