Home>Article>Java> Summary of Java multi-threaded programming methods (with examples)

Summary of Java multi-threaded programming methods (with examples)

不言
不言 Original
2018-09-27 14:38:44 2006browse

This article brings you a summary of Java multi-threading implementation methods (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. When to use multi-threaded programming

A task is executed in sequence under normal circumstances, but if there are multiple similar process blocks in the current task (such as for, while statements) , we can consider extracting these code blocks to run in parallel without blocking

2. Several ways to implement multi-threading

One is to inherit the Thread class and rewrite the run method, and the other is to inherit the Thread class and rewrite the run method. The first is to implement the Runnable interface and rewrite the run method

Starting multi-threads is often used to handle concurrent processes. At this time, for some business needs that do not require high real-time performance, we can also implement queues. Asynchronous implementation.

3. Example

Inherit Thread

/** * * @ClassName: ThreadByEx * @Description: TODO * @author Mr.jqCheng * @date 2018年9月26日 * */public class ThreadByEx extends Thread{ @Override public void run() { // TODO Auto-generated method stub System.out.println("我是继承线程"); } }

Implement Runnable

/** * * @ClassName: ThreadByRunnable * @Description: TODO * @author Mr.jqCheng * @date 2018年9月26日 * */public class ThreadByRunnable implements Runnable{ /*public ThreadByRunnable() { this.run(); // TODO Auto-generated constructor stub }*/ public void run() { // TODO Auto-generated method stub System.out.println("我是实现进程"); } }

Test:

/** * * @ClassName: Test * @Description: TODO * @author Mr.jqCheng * @date 2018年9月26日 * */public class Test { public static void main(String[] args) { // 继承Thread启动的方法 ThreadByEx t1 = new ThreadByEx(); t1.start();// 启动线程 // 实现Runnable启动线程的方法 ThreadByRunnable r = new ThreadByRunnable(); Thread t2 = new Thread(r); t2.start();// 启动线程 //new ThreadByRunnable(); } }

Running result:

I am an inherited thread
I am an implementation process

ok, the simple multi-thread implementation is completed. When start() is called, the process has entered the executable state and is waiting for system execution.

Several common methods of thread processing:

void interrupt(): Send an interrupt request to the thread, the interrupt status of the thread will be set to true, if the current thread is blocked by a sleep call , then an interruptedException will be thrown.

static boolean interrupted(): Test whether the current thread (the thread currently executing the command) is interrupted. Note that this is a static method. Calling this method will have a side effect, that is, it will reset the interrupt status of the current thread to false.

boolean isInterrupted(): Determine whether the thread has been interrupted. The call of this method will not have side effects, that is, it will not change the current interruption status of the thread.

static Thread currentThread(): Returns the Thread object representing the current execution thread.

Daemon process

Used to serve all threads under all other current processes that are not service processes

Just implement deamon.setDaemon(true), before the thread is started Enable

Example

package com.orange.util; /** * * @ClassName: Test * @Description: TODO * @author Mr.jqCheng * @date 2018年9月26日 * */ public class Test { public static void main(String[] args) { Thread deamon2 = new Thread(new DaemonRunner2(), "otherRunner"); deamon2.start();// 启动线程 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread deamon = new Thread(new DaemonRunner(), "DaemonRunner"); // 设置为守护线程 deamon.setDaemon(true); deamon.start();// 启动线程 } static class DaemonRunner implements Runnable { public void run() { // TODO Auto-generated method stub try { Thread.sleep(300); Thread t = Thread.currentThread(); System.out.println(t); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("进入守护线程,说明现在还有其他线程在执行"); } } } static class DaemonRunner2 implements Runnable { public void run() { // TODO Auto-generated method stub try { Thread.sleep(1500); System.out.println("我是其他线程"); } catch (Exception e) { e.printStackTrace(); } } } }

Execution result:

Thread[DaemonRunner,5,main]
Enter the daemon thread, indicating that there are other threads executing
I am other threads

First, start other threads first, which takes 1500ms. At the same time, after the main thread takes 1000ms, it starts to enter the daemon thread. At this time, other threads are still running. When it comes to the daemon thread, it takes time 300ms, other threads are still executing, continue down, the daemon thread has completed execution

But if I change the daemon thread's 300ms to 500ms, what will happen?

There have been two situations, after all, at the critical value

1. I am another thread

2.Thread[DaemonRunner,5,main]
Enter the daemon thread , indicating that there are other threads executing now
I am another thread

The above is the detailed content of Summary of Java multi-threaded programming methods (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn