Java-based multithreading and java-based Multithreading

Source: Internet
Author: User

Java-based multithreading and java-based Multithreading

First, we need to analyze the concepts of processes and threads:

A process is a process of program execution. It holds resources and threads and is dynamic to the program itself.

A thread is the smallest Execution Unit in the system. A single process may have multiple threads that share the resources held by the process. Thread communication is also called thread interaction. The main methods are mutex and synchronization. Synchronization means that a job is completed by the threads through collaboration, and the threads are ordered. mutex means that the threads compete for a resource and only one thread can access the resource at a time.

After introducing these basic concepts, we will briefly introduce Java's support for multithreading.

Java implements multi-threaded operations through the Thread class and interface Runnable. They all have a run method to specify the code executed when the thread is working.

Common Methods of Thread classes

Category

Name

Introduction

Thread Creation

Thread ()

 

Thread (String name)

Thread (Runnable target)

Thread (Runnable target, String name)

Thread method

Void start ()

Start thread

Static void sleep (long millis)

Thread sleep

Static void sleep (long millis, int nanos)

Void join ()

After a thread calls the join method, it causes other threads to wait for the thread to terminate.

Void join (long millis)

Void join (long millis, int nanos)

Static void yield ()

The currently running thread immediately releases the processor and rejoins the queue of the competing processor.

Get thread reference

Static Thread currentThread ()

Returns the reference of the currently running thread.

 

There are two ways to use multithreading: one is to directly inherit the Thread class and the other is to implement the Runnable interface.

Inherit Thread classIn the run method, specify the code to be executed by the thread and use the getName () and setName () methods to access the thread name. Use the start method of the Class Object to enable the thread.

Implement the Runnable interface, Override the run method to specify the code to be executed by the Thread, and then pass the object of this class as a parameter to the Thread object for execution.

In addition, the member variables declared by the volatile keyword can ensure that the current thread can correctly read the value of this member variable after other threads modify this member variable.

 

Properly stop Java threads

Incorrect stop method: stop ()

To stop the thread correctly, use the correct exit flag.

The original intention of using the interrupt () method is not to stop the thread. Normally, you can call this method to set the interrupt mark to TRUE. However, when a thread is blocked because it calls methods such as wait () or sleep (), the interrupt () method cannot be correctly set to TRUE, in addition, an interrupt exception is thrown.

 

Contention condition: when multiple threads share access to the same memory data at the same time, each thread attempts to operate on the data, resulting in data destruction.

Thread mutex and Synchronization

Mutex means that only one thread can operate on the critical section at the same time. In JAVA, you can use synchronized blocks or methods.

In the synchronized block, you need to lock an object and put the code that requires mutual exclusion into the synchronized block to achieve mutual exclusion. The process that obtains the lock can enter the synchronized block.

Synchronization is a communication mechanism between threads, through which the thread execution sequence can be defined. For example, when a thread does not meet the requirement to access a resource, the lock object can be called. the wait () method allows the thread to let out the CPU, let out the lock, and wait in the waitset of the lock object. When the conditions are met, you can wake up a thread randomly using the lock. Y () method, and lock. notifyAll () will wake up all threads in the waitset so that they can compete for the lock again. The wake-up thread continues to run from the wait () method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.