1. Summary of this week's study1.1 Summarize multithreading related content in the way you like (mind map or other).
2. Written work
Multi-thread of this PTA job title set
1. Source code reading: Multi-threaded BouncethreadWhat is the use of the 1.1 ballrunnable class? Why do I need to call Thread.Sleep to hibernate in my code?
A: The Ballrunnable class implements the Runnable interface for performing multi-threaded tasks, moving the ball through the loop of move and repaint in run, and sleeping is to increase the time it takes to complete the method, or the motion trajectory of the ball is not clearly visible.
1.2 Ball.java Only do two things, what are the two things respectively? What does the Ballcomponent object do? What is the use of the ArrayList inside? How many ballcomponent objects are generated during the program run? The program uses multithreading techniques, each of which is drawn in separate threads?
A: Ball.java defines the move function and the Getshape function, one for the motion of the ball, one for the size and coordinates of the ball, the Ballcomponent object for adding the ball, the ArrayList for storing the generated ball, and a total of a Ballcomponent object; Multithreading technology, each call will generate a new ball, is drawn in different programming.
2. Experiment Summary: Problem sets (multithreading)2.1 Topics: Thread, Printtask, runnable, and anonymous inner classes.
and answer: A) What is the benefit of implementing multithreaded programs by defining the implementation class of the Runnable interface than by inheriting from the thread class? b) 6-1,6-3,6-11 experimental summary.
A: You can also inherit other classes after implementing the Runnable interface, you can inherit more conditions, and runnable can allow multithreading to perform tasks together.
6-1: Create a class mythread, write a constructor, use a for loop to output an integer that specifies the number of times, and finally print the identity information.
6-3: According to the requirements of the output can be three times.
6-11: Similar to 6-1, create a class Printtask, write a constructor, use a for loop to output a specified number of integers, and finally print the identity information.
2.2 Using a lambda expression to overwrite 6-3
For:
new Thread(() -> { System.out.println(mainThreadName); System.out.println(Thread.currentThread().getName()); System.out.println(Arrays.toString(Thread.class.getInterfaces())); });
2.3 Title: 6-2 (runnable and stop thread). Answer: How do I need to properly stop a running thread?
A: In 6-2, use the while loop to run the thread continuously, using flag as the flag bit of the stop thread, and stopping the thread when flag is modified.
3. Mutually exclusive access3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)
For:
4. Mutually exclusive access and synchronous access
Complete episode 6-4 (mutually exclusive access) with 6-5 (synchronous access)
4.1 In addition to using the synchronized modification method to achieve mutually exclusive synchronous access, what else can use synchronized to achieve mutually exclusive synchronous access, using code description (please show the relevant code and number)?
A: You can also use the synchronization method block
public voidsend(int number){ synchronized(this){ this.balance+=number; }}//201621123014吴延彬publicvoidback(int number){ synchronized(this){ this.balance-=number; }
4.2 What is the difference between a synchronous code block and a synchronization method?
A: The synchronous method directly modifies the whole method with synchronized, locking the whole method, while the synchronous code block is locked in some places within the method, and the range is small.
4.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?
A: The principle is to enter the method by obtaining a unique lock, and return the lock after execution. When the lock has been removed, other threads are not allowed to enter the method, only wait until the lock is returned before the next thread can continue to acquire the lock entry method.
When the program executes the synchronized synchronous code block or synchronous method, the line enters upgradeable enters the runnable, if the lock is still there, it gets the lock into the running, if the lock has been taken away then enters the wait pool, and then enters the lock pool after notify (), The operation after the lock is returned.
4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?
A: Use Wait () and notify () to implement thread collaboration.
5. Inter-Threading cooperation: producer-consumer issues5.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?
A: Not normal, the normal situation should be inventory is 0, but in fact, multiple runs often appear in inventory of 100, in order to simplify the output, I try to save the case is 3, the first run is normal, but the second time the inventory is 3 abnormal situation, the comparison found that the second run, The operation of the goods will be taken out first, but no goods can be removed, so the display fails to be removed, and then the goods are placed, so there will be 3 inventory in the final case. After observation, the reason is that there is no synchronization method.
5.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
For:
6. Object-oriented design job-Library management system
Not complete this topic for the time being
3. Code Cloud and PTA
Topic Set: Multithreading
3.1. Code cloud codes Submission record
In the Code cloud Project, select statistics-commits history-set time period, and then search for and
Several elements must appear: Submit Date-user name (name and school number)-no instructions
3.2 "Multi-threaded" PTA Submission list
Two graphs are required (1. Ranking chart. 2.PTA Submission list diagram)
3.3 count The amount of code completed this week
The weekly code statistics need to be fused into a single table.
| Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
| 1 |
0 |
0 |
0 |
0 |
| 2 |
0 |
0 |
0 |
0 |
| 3 |
313 |
313 |
9 |
9 |
| 4 |
641 |
328 |
20 |
11 |
| 5 |
84W |
305 |
25 |
5 |
| 6 |
1451 |
505 |
32 |
7 |
| 7 |
1969 |
518 |
35 |
3 |
| 8 |
2284 |
315 |
43 |
8 |
| 9 |
2682 |
344 |
47 |
4 |
| 10 |
2938 |
256 |
55 |
8 |
| 11 |
3196 |
258 |
62 |
7 |
201621123014 Java programming 11th Week of study summary