Java Learning log (thread)

Source: Internet
Author: User

1. Processes and Threads


Process: run a speed in which the program allocates the running space in memory

Threads: Run in a process and can switch between multiple threads.


The relationship between the problem process and the thread?

processes include threads.


2. Single-threaded and multithreaded (JAVA preemption)

Single thread: There is only one path that must wait for the first execution to execute the second one. like waiting in line for dinner ....  


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/4B/wKioL1cZEI6Q2bqoAACmJG9Hjn8226.jpg "title=" 14591490369778.jpg "alt=" Wkiol1czei6q2bqoaacmjg9hjn8226.jpg "/>

Multithreading: There are multiple paths ... for example, the canteen queue of multiple windows.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/4B/wKioL1cZEKqBFKMRAAChU9HRIVc418.jpg "title=" 14591490457673.jpg "alt=" Wkiol1czekqbfkmraachu9hrivc418.jpg "/>


3. Threads are divided into main thread and child thread **

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/4B/wKioL1cZELfRa0TyAAB0oB8ls0Y507.jpg "title=" 14591490508765.jpg "alt=" Wkiol1czelfra0tyaab0ob8ls0y507.jpg "/>

The main thread is equivalent to the major task in the game.

A sub-thread is equivalent to a branch task in the game.


List of questions:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/4C/wKiom1cZEAWB9QLAAAInSqasVeI894.jpg "title=" 14591490593534.jpg "alt=" Wkiom1czeawb9qlaaainsqasvei894.jpg "/>


4. Priority level of threads  

Java has ten levels,1 to ten levels

from low to high, but it is not compatible with the operating system well, such as winw2000e 7 priority levels, and all the non-fixed hope to be ported to each platform, it is recommended to use Max-priokit

Min-priokit

Norn-priokit

Note: high levels are more opportunities for execution

Low level is to get fewer execution opportunities


5. Problem area

Question : Why do you want to make a copy of the Run method??

The Start method automatically calls the run method, and the run method automatically joins some of the features you want.  

Question : What is the difference between calling the run method directly and the method that calls stat ?  

Run () method: Normal method cannot start thread

Start() method : starts a thread and automatically calls the run method

Public final void Join ()

Throws Interruptedexception

Waits for the thread to terminate.  

When the thread terminates, the thread ends before it can execute

public static void yield () pauses the currently executing thread object and executes other threads.

comity, pausing the currently executing program object and letting other threads execute to the same priority or higher.     

public static void sleep (Long millis,int Nanos)

throws Interruptedexception allows the currently executing thread to hibernate (suspend execution) in the specified number of milliseconds plus the specified number of nanoseconds, and this operation is affected by the accuracy and accuracy of the system timer and scheduler. The thread does not lose ownership of any monitors

Daemon Threads

Setdaemon ( boolean ) setting is a daemon thread

that is, the foreground completes the background to complete

For example: garbage collection mechanism

End Thread

1) Change Flag

2) interrupt()

3)break thread in public void interrupt () .

If the current thread does not interrupt itself (which in any case is allowed), then the thread's checkAccess method is called, which may throw SecurityException.  

3) throw an exception

Note : try not to use stop (too overbearing will directly terminate all runs ) and

Issues with concurrent access

Inaccuracy of data

Synchronized Synchronization

Synchronized (this) synchronization block; This synchronizes the current object

Synchronized Synchronization Method

dead Lock  !

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/4B/wKioL1cZEQmCSsW7AAC-hWjskBg382.jpg "title=" 14591490860316.jpg "alt=" Wkiol1czeqmcssw7aac-hwjskbg382.jpg "/>



Definition: Deadlock is the short name of the process deadlock,

For example :

On a river there is a bridge, the bridge is narrow, can only accommodate a car through, unable to two cars parallel. If there are two cars a and b respectively by the bridge at both ends of the bridge, then for A car, it went through the bridge on the left side of the road (that is, a part of the bridge resources), to cross the bridge must wait for the B car to make the right deck, At this time a car can not go forward, for the b car, it went through the bridge on the right side of the road (that is, a part of the bridge's resources), to cross the bridge must wait for A car to make the left side of the bridge, at this time B car can not advance. Both sides of the car are not reversing, resulting in waiting for each other to let out the bridge deck, but no one will give way, would endlessly wait to continue

The life cycle of a thread

NEW: Create thread object

ready: There is executive qualification, no execution right,start ()

Operational : has the executive qualification, has the executive right

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/4B/wKioL1cZEVGCt1XDAAA5PzlhtMY920.jpg "title=" 14591490720882.jpg "alt=" Wkiol1czevgct1xdaaa5pzlhtmy920.jpg "/>



death: Thread object becomes garbage, waiting to be recycled

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7F/4C/wKiom1cZEOWDCULOAADZPUzUoGk912.jpg "title=" 14591490677028.jpg "alt=" Wkiom1czeowdculoaadzpuzuogk912.jpg "/>



This article is from "Doyle" blog, please make sure to keep this source http://conandoyle.blog.51cto.com/11462365/1766509

Java Learning log (thread)

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.