Common methods of multithreading
Thread to stop
-
Stop () and destroy() -> provided by the JDK are deprecated and not recommended
-
The thread stops by itself
- Using the number of times
- Sign a
Example flag bits:
public class ThreadStopDemo {
public static void main(String[] args) throws InterruptedException {
ThreadStop threadStop = new ThreadStop();
new Thread(threadStop, "a").start();
TimeUnit.SECONDS.sleep(2); threadStop.stop(); }}class ThreadStop implements Runnable {
private boolean flag = true;
@Override
public void run(a) {
while(flag) {
System.out.println(Thread.currentThread().getName() + "Thread executing!"); }}/** * provides a method to change flag to false to stop the thread */
public void stop(a) {
this.flag = false;
System.out.println("Thread stopped"); }}Copy the code
Thread to sleep
Sleep () method
Thread.sleep(1000);
TimeUnit.SECONDS.sleep(1);
Copy the code
-
Exception is thrown
-
After the sleep time is reached, the thread enters the ready state
-
Sleep can simulate network delay and countdown
-
Every object has a lock, and sleep does not release the lock
Analog network delay
// Simulate network delay
public class ThreadSleep {
Sleep () does not release the lock. Sleep () needs to catch exceptions */
public static void main(String[] args) throws InterruptedException {
System.out.println("Tickets on sale");
BuyTicketThread buyTicketThread = new BuyTicketThread();
new Thread(buyTicketThread, "Zhang").start();
new Thread(buyTicketThread, "Bill").start();
new Thread(buyTicketThread, "Fifty").start(); }}class BuyTicketThread implements Runnable {
private Integer ticketNum = 20;
@Override
public void run(a) {
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
while (ticketNum > 0) {
System.out.println(Thread.currentThread().getName() + "Buy the number" + ticketNum-- + "Ticket"); }}}Copy the code
Thread comity
- Allows the currently executing thread to pause but not block
- Changes a thread from a running state to a blocked state
- CPU rescheduling, comity may not succeed.
Unyielding test code:
public class ThreadYield {
public static void main(String[] args) throws InterruptedException {
YieldA yieldA = new YieldA();
YieldB yieldB = new YieldB();
new Thread(yieldA, "A").start();
new Thread(yieldB, "B").start(); }}class YieldA implements Runnable {
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName() + "Thread executing");
System.out.println(Thread.currentThread().getName() + "Thread end execution"); }}class YieldB implements Runnable {
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName() + "Thread executing");
System.out.println(Thread.currentThread().getName() + "Thread end execution"); }}Copy the code
Output result:
Thread comity test:
class YieldA implements Runnable {
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName() + "Thread executing");
Thread.yield();
System.out.println(Thread.currentThread().getName() + "Thread end execution"); }}class YieldB implements Runnable {
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName() + "Thread executing");
System.out.println(Thread.currentThread().getName() + "Thread end execution"); }}Copy the code
Output result:
Thread surrender succeeded
The thread forces -join
Thread.join () forces the execution of thread. When the execution of thread ends, other threads continue to execute.
(in)
Normal multithreading tests:
public class ThreadJoin implements Runnable {
@Override
public void run(a) {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":"+ i); }}public static void main(String[] args) {
Runnable target;
Thread thread = new Thread(new ThreadJoin(), "A");
thread.start();
for (int i = 0; i < 100; i++) {
System.out.println("Main thread:"+ i); }}}Copy the code
Execution Result:
The main thread:0The main thread:1The main thread:2The main thread:3The main thread:4
A:0The main thread:5The main thread:6The main thread:7The main thread:8The main thread:9The main thread:10The main thread:11The main thread:12
A:1The main thread:13The main thread:14The main thread:15The main thread:16The main thread:17
A:2The main thread:18
A:3The main thread:19
A:4The main thread:20The main thread:21
A:5. .Copy the code
You can see that thread A alternates with the main thread.
Add code thread.join() to the main thread;
public class ThreadJoin implements Runnable {
@Override
public void run(a) {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":"+ i); }}public static void main(String[] args) throws InterruptedException {
Runnable target;
Thread thread = new Thread(new ThreadJoin(), "A");
thread.start();
for (int i = 0; i < 100; i++) {
if (i == 20) {
thread.join();
}
System.out.println("Main thread:"+ i); }}}Copy the code
Execution Result:
The main thread:1The main thread:2The main thread:3The main thread:4The main thread:5The main thread:6The main thread:7
A:0
A:1
A:2
A:3
A:4
A:5
A:6The main thread:8The main thread:9. .// i == 20The main thread:19
A:10
A:11
A:12. . A:48
A:49
Copy the code
Thread A alternates with main thread until I ==20. When I ==20, thread A is forced to terminate.