Java Multithreading Basics

Source: Internet
Author: User

A thread is a unit of execution within a process, and there are several threads in the process.

Usually the main thread or process is blocked sequentially, if you want to perform some subtasks asynchronously, rather than blocking the execution of the current thread, you need to create child threads, the main thread can wait for their results after the child threads are created, and get their results after other operations, but you can leave it to fend for itself.

Here's a look at how to create multiple threads and how to get the child main thread to interact:

1, in the end is start or run

The direct call to the Run method does not open a new thread, it should call the Start method, and the Start method actually calls the Run method on a new operating system thread. In other words, calling the Run method directly instead of invoking the Start method does not open a new thread, but instead executes your operation in the current thread that calls run, for example:

Thread thread = new Thread("t1"){    @Override    public void run()    {        // TODO Auto-generated method stub        System.out.println(Thread.currentThread().getName());    }};thread.start();//这样会输出 "t1" Thread.run ();//This will output "main" without opening a new thread 2, terminating the thread

Thread.stop () is not recommended for use. It will release all monitor,stop methods too "violent", and it will stop the thread immediately, no matter where the thread is executed.

3. Thread Interrupt

Instead of terminating the thread, you should use a break path.

Interrupts are a collaborative mechanism. This means that invoking the thread object's interrupt method does not necessarily break the running thread, it simply requires the thread to break itself at the right time. Each thread has a Boolean interrupt state (not necessarily the property of the object, in fact, the state is indeed not a thread's field), and the interrupt method simply resets the state to true. For non-blocking threads, only the interrupt state is changed, that is, thread.isinterrupted () returns true and does not stop the program;

4. Thread hangs

Suspend (suspend) and resume (resume) threads

    • Suspend () does not release the lock
    • If the lock occurs before the resume (), the deadlock occurs

Both of these methods are deprecated methods and are not recommended for use.

The reason is that suspend does not release the lock, so no thread can access the critical section resource that it is locked in until it is resume by another thread. Because there is no way to control the order in which threads run, if the resume method of another thread is first run, then the suspend that runs after it will always occupy this lock, causing a deadlock to occur.

5. Join and Yeild

Yeild is a native static method that frees up the CPU time it occupies, and then competes with other threads (note that the yeild thread is likely to compete for the CPU, notice the difference from sleep). It is also stated in Javadoc that Yeild is a basic method that is not used, and is generally used in debug and test.

The Join method means waiting for the other thread to end, just like the code in the Suspend section, so that the main thread waits for the t1,t2 to end. Without the end, the main thread is stuck there.

The join () method can also pass a time, meaning to wait for a period of time, and automatically wake up after that.

6. Daemon Thread

setDaemon(true);就可以将线程设置为守护线程

The daemon thread is silently doing some systematic service in the background, such as garbage collection thread, JIT thread can be understood as the daemon thread.

7. Thread Priority

There are 3 variables in the thread class that define the thread priority.

Public final static int min_priority = 1;
Public final static int norm_priority = 5;
Public final static int max_priority = 10;

Let a high-priority thread and a low-priority thread compete for a lock at the same time to see which one completes first.

Certainly not necessarily high priority must be completed first. After several runs, it is found that the probability of high priority completion is large, but the low priority is still possible first.

8. Basic thread Synchronization operations

Synchronized and object.wait () obejct.notify ()

Reference article: http://www.importnew.com/21239.html

Java Multithreading Basics

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.