Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

A process executed by multiple threads.

Shulou Source: shulou.com Published: 2022-06-02 03:21:39 11月01日 Update

This article mainly explains "the process of multithreaded execution". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the process of multithreaded execution.

1. The main thread waits for the child thread to complete its execution before executing-- Thread.join ()

Vector vector = new Vector (10); for (int I = 0; I < 10; iTunes +) {MyThread myThread = new MyThread ("id" + I, "name" + I, roles); Thread t = new Thread (myThread); vector.add (t); t.start () } for (Thread thread: vector) {try {thread.join ();} catch (InterruptedException e) {e.printStackTrace ();}}

2. Java multithreading-- let the main thread wait for the child thread to finish execution (CountDownLatch class)

Runable interface implementation class

Import com.winning.jcfw.core.util.SpringUtil;import com.winning.jcfw.empi.dao.DaGrJbxxDao;import com.winning.jcfw.empi.entity.PatientEntity;import com.winning.jcfw.empi.service.RegisterEmpiService;import com.winning.jcfw.empi.service.impl.RegisterEmpiSerImpl;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang.StringUtils;import java.util.List;import java.util.concurrent.CountDownLatch / * * @ ClassName DaGrJbxxEmpiRunnable * @ Description Public Security Table DA_GR_JBXX thread executes single generation EMPI * @ Author WJX * @ Date 17:45 * * / @ Slf4jpublic class DaGrJbxxEmpiRunnable implements Runnable {private RegisterEmpiService registerEmpiService = SpringUtil.getBean (RegisterEmpiSerImpl.class); private DaGrJbxxDao daGrJbxxDao = SpringUtil.getBean (DaGrJbxxDao.class); private CountDownLatch dLatch; / * Mechanism Code Collection * / private List jgList Public DaGrJbxxEmpiRunnable (List threadJgLsit,CountDownLatch downLatch) {super (); jgList = threadJgLsit; dLatch = downLatch;} @ Override public void run () {for (int ionometer I 0) {s = null; for (Node t = tail; t! = null & & t! = node T = t.prev) if (t.waitStatus {System.out.println ("blocking Thread 1"); LockSupport.park (); System.out.println ("Thread 1 finished");}); t.start () Try {Thread.sleep (2000); System.out.println ("Wake up Thread 1"); LockSupport.unpark (t); Thread.sleep (5000); System.out.println ("main thread ends") } catch (InterruptedException e) {e.printStackTrace ();}}

Blocking thread 1

Wake thread 1

Thread 1 finished execution.

The main thread ends

Thank you for your reading, the above is the content of "the process of multithreaded execution". After the study of this article, I believe you have a deeper understanding of the process of multithreaded execution, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Thread method blocking process mechanism code learning generation content situation interface identity counter update same success following number medical medical institution Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux macOS Shulou Tech Info Microsoft