1. Summary of this week's study
1.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 Bouncethread
What is the use of the 1.1 ballrunnable class? Why do I need to call Thread.Sleep to hibernate in my code?
For:
The Ballrunnable class implements Runnable interface to the thread task, in which the move () and the repaint () function are called to implement the motion trajectory.
Calling Thread.Sleep to hibernate is to delay the thread's completion time so that we can see the movement of the ball.
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?
For:
- The definition implements the Move () function; the Getshape () function is defined to obtain the size and coordinate position of the ball;
- Ballcomponent, completed the "Add Small ball", "draw the ball" two tasks;
- Use ArrayList to store small balls;
- A Ballcomponent object was generated;
- Each time the Start,addball method starts a new thread, each ball is drawn in its own thread.
1.3 Choose: Program rewrite: When the program runs, each ball is from a fixed position. How do you rewrite the program so that when you click Start, each ball can be moved from a different position, with a different step?
1.4 Choose: The trajectory of the different balls is exactly the same. The transformation process, so that each small ball trajectory is not exactly the same, such as some can take the cosine trajectory, some can go square wave trajectory, some go Brownian motion, some of the pentagram, and so on
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.
For:
Realize the benefits of multithreaded programming by defining an implementation class for the Runnable interface:
In this way, the thread class can inherit other classes while implementing the Runable interface. Multiple threads can share the same target object, so it is more appropriate to process the same resource than by inheriting from the thread class.
-
6-1
The subject uses the extends thread to create the thread, first the constructor (the argument), and then the Run method's rewrite: note here to "Use System.out.println (Thread.CurrentThread (). GetName () + "" +isalive ()) Print identification information "placed outside the loop body.
-
6-3
The subject requires an anonymous class to implement the override of the Run method, with the thread name of the thread mainThreadName T1:Thread.currentThread().getName()
-
6-11
In addition to implementing the Runnable interface, the rest is the same as 6-1.
2.2 Using a lambda expression to overwrite 6-3
For:
Thread t1 = 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?
For:
Threads can be terminated by calling the thread's Stop () method, but some problems may arise that do not guarantee that the thread's resources are properly freed and have been discarded .
If you want to stop a thread, you should let the thread run out of the expected process and use the Boolean flag to exit the run () method in the thread
//线程自我终止public void run(){ while (flag){ … //完成线程的特定功能 if( time_to_die ){ flag = false; } }}
2.4 Selected: 6-8 (countdownlatch) Experiment Summary
2.5 Select: 6-9 (set synchronization problem) experimental summary
2.6 Choose: More difficult: 6-10 (callable), and answer why Runnable need callable? Experimental summary.
3. Mutually exclusive access
3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)
For:
3.2 Optional: Further use of the actuator to improve the corresponding code (key code, need to appear number)
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)?
For:
You can also use the synchronized code block; Take 6-4 as an example
4.2 What is the difference between a synchronous code block and a synchronization method?
For:
Synchronization method directly on the method plus synchronized implementation lock, synchronous code block is locked inside the method, so: Synchronization method lock range is larger, and the synchronization code block range to small point, the general synchronization of the scope of the larger, the performance is worse, the performance is good.
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?
Secretary
Principle: Use synchronized to lock the shared resources so that only one thread can use the resources at the same time;
From the running result, only one thread can be executed in a single time. When TA works, it is equivalent to TA give a for loop Shangli a lock, TB must wait for TA to execute this block of code to execute the code block.
4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?
For:
Synchronized, wait, Notify/notifyall.
5. Inter-Threading cooperation: producer-consumer issues
5.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?
For:
Not normal, put in 100, take out 100, it is impossible to have 10 left. The reason is that producer and customer execution speed is different, that is, the order of access to the goods each run will be different, there will be errors.
5.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
For:
5.3 Select: Use lock with the condition object to resolve the problem.
6. Object-oriented design job-Library management system
6.1 The system's functional modules table, which reflects the owner of each module.
6.2 Running the video
6.3 Explain the module you are responsible for, and paste the key code of your own module (the number and name of the school).
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 |
619 |
619 |
12 |
12 |
| 4 |
619 |
0 |
12 |
0 |
| 5 |
970 |
351 |
17 |
5 |
| 6 |
1118 |
148 |
25 |
8 |
| 7 |
1231 |
113 |
28 |
3 |
| 8 |
1707 |
476 |
33 |
5 |
| 9 |
1902 |
195 |
38 |
5 |
| 10 |
2316 |
414 |
43 |
5 |
| 11 |
2505 |
189 |
48 |
5 |
| 12 |
2885 |
380 |
54 |
6 |
201621123057 Java programming 11th Week of study summary