Threading mechanism for Java

Source: Internet
Author: User

One, two ways of implementing multithreading in Java
1) inherit the thread class
The thread class contains everything you need to include and create threads. The most important method of thread is run (), which needs to overwrite the run () method when writing a thread program, and the Run () method runs in parallel with other threads, so there are resource contention and synchronization issues. The thread also contains a special method of Start (), which is used to initialize the thread in a special way and then call Run ().
The Start method is called to start the thread, and the Run method is just a normal method call to thread, or it executes in the main thread. The code that needs to be processed in parallel should be placed in the run () method, and the start () method will automatically invoke the run () method, as specified by the JVM's memory mechanism. and the run () method must be public access, and the return value type is void:
2) Implement Runable interface
The Runable interface has only one function run (), and the function must be implemented by the class that implements the interface. When using the Runable interface, you cannot directly create an object of the desired class and run it, but must run from within an instance of the Thread class.

Second, thread state
There are 4 states in the thread life cycle: New state, executable state, death state, and blocking state. which
New State: The thread has been created, and start () has not yet been called;
executable State: The thread can execute, waiting for the CPU time to be allocated to the thread;
Death Status: The thread dies after run () returns. Either the call to the Stop () or the Destroy () method will also cause the thread to die, but stop () will produce an exception, and destroy () is forced to terminate so that no lock is released;
Blocking State: The thread will not be allocated CPU time and cannot be executed.

The blocking state is more complex and the threadreasons for entering the blocking stateThe main:
1) Call Sleep (ms), go to sleeping
2) call suspend () to suspend the thread. (Call Resume () to resume running)
3) call Wait () to pause the thread. (Call Notify () or Notifyall () to resume running)
4) I/O blocking
5) Call the synchronization method of another object that is in a locked state

Third, the lock of the object
All objects in Java have implicitly associated monitors, which are "locks." The program does not have to generate a lock object, because any object has a lock, and the This object is usually used in the program.
The methods associated with locks are:
1) Wait ()
The thread that called the method waits until the lock on the object is obtained, and the thread can continue execution.
2) Wait (long) and wait (long,int)
The passed parameter specifies the maximum time the thread can wait, and if the lock is not received within the specified time, an exception interrupedexception is thrown, and the program continues to run after the exception has been processed.
3) Notify () and Notifyall ()
Calling the Notify () or Notifyall () method on the lock object can release the lock on the object. where notify () wakes up only one thread waiting for the lock, and Notifyall () wakes up all the threads that are waiting for the lock, but which of the awakened threads is determined by the virtual machine.

Iv. Synchronization of Threads
The synchronized statement synchronizes the access of multiple threads to a particular piece of code by using an object lock, and at any time only one thread can execute the protected code.
Synchronized (taskqueue) {            newtask.settaskid (++taskcounter);            Newtask.setsubmittime (New Date ());            Taskqueue.add (newtask);            /* Wake up queue, start execution *            /Taskqueue.notifyall ();        }
In the above example, synchronized (taskqueue) is executed, and the thread must acquire the lock of the object taskqueue. If the thread does not acquire a lock on the object taskqueue, the thread stops at the synchronized instruction. The thread will return the lock to the object taskqueue until the thread leaves the code snippet protected by the synchronized directive.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Threading mechanism for Java

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.