Multithreading (ii)

Source: Internet
Author: User

1. Lock operation and release operation for thread after JDK5
A: In order to express more clearly where Hega and release lock B:lock Lock (): Locking Unlock (): Release lock C:reentrantlock is the implementation class of Lock lock = new Reentrantlock (); Lock.lock ();//locking ...//Lock code Lock.unlock ();//Release lock
2. Description and code representation of deadlock problem
Refers to two or more two threads in the execution process, because of contention for resources to produce a mutual wait phenomenon (if there is a synchronous nesting, it is easy to create a deadlock problem)
3. Multi-threaded representation of producers and consumers (inter-thread communication issues)
A: Different kinds of threads for the same resource operation code implementation://In the outside world to create the data, through the construction method passed to other classes Student s = new Student (); GetThread GT = new GetThread (s); Setthread st = new Setthread (s); B: Resources to be implemented as a resource class: Student Set Data class: Setthread (producer) Get Data class: GetThread (consumer) test class: Studentdemo code: A: The most basic version, only one data. B: Improved version, given different data, and added synchronization mechanism C: Wait for the wake-up mechanism to improve the program, so that the data can be implemented in turn wait () notify () Notifyall () (Multi-production and multi-consumption) D: Wait for the wake-up mechanism of code optimization. The data and operations are written in the resource class Note: These method calls must be called by the lock object, and the lock object we just used is an arbitrary lock object. Therefore, these methods must be defined in the object class.
4. Thread Group
Combine multiple threads together. It can classify a batch of threads, and Java allows the program to control the thread group directly. Public final Threadgroup Getthreadgroup (): Returns the thread group to which the thread belongs Threadgroup TG = Mt.getthreadgroup ();p ublic final String getName () : Returns the name of this thread group string name1 = Tg.getname (); Note: By default, all threads belong to the same group Threadgroup (String name): Modify the group where the thread resides myrunnable my = new Myrunnable (); Threadgroup TG2 = new Threadgroup ("Newthreadgroup"); Thread t = new Thread (TG2, My, "threadname"); Tg2.get ...
5. Thread pool
The program starts a new thread at a higher cost, and using the thread pool can improve performance, especially if you are creating a large number of short-lived threads, you should consider using threading pooling. Line constructor Each thread code ends and does not die, but returns to the thread pool again to become idle, waiting for the next object to be used. Before JDK5, we had to implement our own thread pool manually, starting with JDK5 and Java built-in support for thread pooling. Thread pool implementation: A: Create a thread pool object that controls how many thread objects to create public static executorservice newfixedthreadpool (int nthreads) B: This thread pool can perform a class implementation that can execute a Runnable object or a thread represented by a callable object Runnable interface C: Call the following method Future<?> submit (Runnable Task) Future Submit (callable Task) D: Do not want to end Pool.shutdown (); Code case: public static void Main (string[] args) {//Create a thread pool object, Control to create several thread objects Executorservice pool = new Executorservice.newfixedthreadpool (2);// You can execute the thread pool.submit (new Myrunnable ());p Ool.submit (New Myrunnable ()), which is represented by the Runnable object or callable object, and/or end thread pool Pool.shutdown ( );}
6, multi-threaded implementation of the third scenario
Implement the callable interface, similar to the runnable code case--10
7, Anonymous internal class way to achieve multithreading
A:new Thread () {code ...}. Start (); B:new Thread (New Runnable () {code ...}) {}.start (); Code case://inherits the thread class to implement multithreaded new thread () {public void run () {...}}. Start ();//implement Runnable interface to implement multithreaded new thread (new Runnable () {public void run () {...}}) {}.start ();
8. Timer
You can perform a task at a specified time and also repeat the dependent timer and Timertasktimer: Timed public timer () public void Schedule (timertask task, long delay) public void Schedule (timertask Task) public void Cancel (); Code case: public static void Main (string[] args) {//Create timer object Timer T = new timer (); /3 seconds after the bombing mission//t.schedule (new MyTask (), 3000);//End Task T.schedule (new MyTask (t), 3000);} Class MyTask extends TimerTask () {private Timer t;public MyTask () {}public mytask (Timer t) {this.t = t;} public void Run () {System.out.println ("Beng!!!"); T.cancel ();}}
9, Multi-threaded face questions
A: There are several implementations of multithreading, which are the different? Inheriting the Thred class to implement the Runnable interface extension: Implementing the Callable interface, which needs to be combined with the thread Pool B: Synchronization There are several ways, what is the difference? Two synchronous code block synchronization methods C: Start a thread is run () or start ()? What's the difference between them? Start (); Run (): encapsulates the code that is executed by the thread, calling only the normal method call Start (): Start the thread, and the difference between the run () method D:sleep () and the Wait () method is automatically called by the JVM Sleep (): time must be specified. Do not release the Lock Wait (): You can not specify a time, or you can specify a time to release the lock. E: Why Wait (), notify (), Notifyall () are defined in the object class because the calls to these methods are dependent on the lock object, and the lock object of the synchronous code block is any lock. F: Thread life cycle New--ready--run--Death new--ready--run--block--ready--run--Death suggestion drawing explanation

Multithreading (ii)

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.