1. Study summary 1.1 This week summarize multithreading related content in the way you like (mind map or other).
2. Written workMulti-thread of this PTA job title setQ1. Mutually exclusive access and synchronous access1.1 In addition to using the synchronized modification method to achieve mutually exclusive synchronous access, what is the way to achieve mutually exclusive synchronous access (please show the relevant code)?
Public intGetBalance () {//synchronizedreturnbalance;} Public voidDepositintMoney ) {//synchronizedLock.Lock();Try{Balance+=Money;plus.signalall ();} finally {Lock. Unlock ();}}
1.2 What is the difference between a synchronous code block and a synchronization method?
The synchronization method is to add a lock by adding the keyword synchronized before the method. The synchronization code block is locked inside the method.
1.3 What is the principle of implementing mutually exclusive access? Use the object lock concept and describe it in conjunction with the corresponding code block. How does the state of the thread change when the program executes the synchronized synchronous code block or synchronous method?
classCounter {Private Static intID =0; PublicSynchronizedStatic voidAddid () {ID++; } PublicSynchronizedStatic voidSubtractid () {ID--; } Public Static intgetId () {returnID; }}
This code cannot get object locks, multiple threads start (), Competition object locks, a thread competes to get object locks, the rest of the threads wait in the pool waiting for threads to release object locks, and when threads release object locks the remaining threads start competing for object locks until the program ends.
1. 4 What keywords are used in Java Multi-threading to implement communication between threads, which in turn enables threads to work together? Why does synchronous access generally have to be put into synchronized methods or blocks of code?
Use the Wait (), notify (), and notifyall () keywords to implement communication between threads, which enables threads to work together, in order to stop the conflicts caused by multiple threads accessing the same resource.
Q2. Alternating executionExperimental SummaryQ3. Mutually exclusive access3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)
//201521123089classCounter {Private Static intID =0; PublicSynchronizedStatic voidAddid () {ID++; } PublicSynchronizedStatic voidSubtractid () {ID--; } Public Static intgetId () {returnID; }}
3.2 Further use of the actuator to improve the corresponding code (key code, need to appear number)
Reference: Java multi-thread executor, Executorservice, executors, callable, future and FuturetaskQ4. Collaboration among threads: producer-consumer issues4.1 Run the Myproducerconsumertest.java. The result of the normal operation should be 0 goods left in the warehouse. Run multiple times, observe the results, and answer: Is the result normal? What's not normal? Why?
Not normal, do not implement the implementation of inter-threading communication, Synergy.
4.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
4.3 Select: Use lock with the condition object to resolve the problem. Q5. Query data answer: What is thread safety? (Use your own words and code to summarize, write your own understanding of the work) 3. Code submission record on code cloud
Topic Set: Multithreading (4-4 to 4-10)
3.1. Code Cloud codes Submission record
In the Code cloud Project, select statistics-commits history-set time period, and then search for and
3.2 Multi-threaded PTA Submission List
201521123089 Java programming 11th Week of study summary