Java multi-threading is something we use very often, and there are many application methods in the process of continuous learning. Let’s learn these key methods below. Hope this helps. First, let’s take a look at the specific method classification.
Methods commonly used in Java multi-threaded programs include the following: run(), start(), wait(), sleep(), notify(), notifyAll(), yield(), join(), and There is an important keyword synchronized. These methods are explained below:
1. run() and start()
These two methods should be familiar. Put the code that needs parallel processing in the run() method, and the start() method starts the thread. The run() method will be called automatically, which is specified by Java's memory mechanism. And the run() method must have public access permissions and the return value type is void.
2. Keyword Synchronized
This keyword is used to protect shared data. Of course, the premise is to distinguish which data is shared data. Each object has a lock flag. When a thread accesses the object, the data modified by Synchronized will be "locked", preventing other threads from accessing it. After the current thread has accessed this part of the data, it releases the lock flag, and other threads can access it.
1.public ThreadTest implements Runnable
2.{
3.public synchronized void run(){
4.for(int i=0;i
5.{
6. System.out.println(" " + i);
7.}
8.}
9.public static void main(String[] args)
10.{
11.Runnable r1 = new ThreadTest ();
12.Runnable r2 = new ThreadTest();
13.Thread t1 = new Thread(r1);
14.Thread t2 = new Thread(r2);
15.t1.start() ;
16.t2.start();
17.}
18.}
The i variable in the above program is not shared data, that is, the Synchronized keyword here does not work. Because the two threads t1 and t2 are threads of two objects (r1, r2). Different objects have different data, so the i variable of the two objects r1 and r2 does not share data.
When the code is changed to the following: the Synchronized keyword will work
19.Runnable r = new ThreadTest();
20.Thread t1 = new Thread(r);
21.Thread t2 = new Thread (r);
22.t1.start();
23.t2.start();
The above is a detailed introduction to Java multi-threading. For more related articles, please pay attention to the PHP Chinese website (www.php. cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor