Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the Java multithreading methods?

Shulou Source: shulou.com Published: 2022-06-02 04:06:11 10月03日 Update

This article mainly introduces "what are the Java multithreading methods". In the daily operation, I believe that many people have doubts about the Java multithreading methods. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the Java multithreading methods?" Next, please follow the editor to study!

First, the interrupt method is a way to let the thread exit.

Import java.util.*; public class TestInterrupt {public static void main (String [] args) {MyThread t = new MyThread (); t.start (); try {Thread.sleep (10000);} catch (InterruptedException I) {} t.interrupt () }} class MyThread extends Thread {public void run () {while (true) {try {System.out.println ("-" + new Date () + "-"); Thread.sleep (1000);} catch (InterruptedException I) {return }}

2. Join and yield methods

The current thread method body will not be executed until the run () method of t.join () / t is finished.

/ / that is, two threads become one thread.

T.yield (); / / pauses the currently executing thread object and executes other threads. Method is static

/ / which thread body executes this method and which thread gives in

Public class TestYield {public static void main (String [] args) {MyThread3 T1 = new MyThread3 ("T1"); MyThread3 T2 = new MyThread3 ("T2"); t1.start (); t2.start ();}} class MyThread3 extends Thread {MyThread3 (String s) {super (s);} public void run () {for (int I = 1teri)

Tags: Threads methods production consumption objects producers learning items baskets security code priorities more tags this method problems assurance synchronization help utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB macOS Huawei Redmi