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?
BallRunnableImplements the Runnable interface.
- When a thread executes too fast, it needs to hibernate, and in this case we can't see the motion trajectory of the ball without sleeping. On the other hand, hibernation is also required in order for other threads to have an opportunity to execute.
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?
Ball.javaThe ball move() is moved getShape() to get the size and position of the ball.
- Add and draw a new ball.
- ArrayList is used to store these balls.
- One.
- Start a thread every time a click starts, so each ball is drawn in its own thread.
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.
- 1. The runnable interface is suitable for multiple threads with the same program code to share the same resource.
2, when we create a thread with a subclass that has inherited a class, because a class cannot have two parent classes at the same time, it cannot be inherited from the thread class, then only the way to implement the Runnable interface can be chosen.
- Experiment Summary:
6-1 is to write the Mythread class inherits from Thread, in Mythread, rewrite the Run method, with the System.out.println(Thread.currentThread().getName()+" "+isAlive()); statement output.
6-3 start a thread, using the System.out.println(Arrays.toString(getClass().getInterfaces())); print three lines of information.
6-11 implements the Runnable interface, outputting the integer 0~n-1 in the Run method.
2.2 Using a lambda expression to overwrite 6-3
2.3 Title: 6-2 (runnable and stop thread). Answer: How do I need to properly stop a running thread?
- The thread class can use
stop() methods to terminate threads, but it is not safe to use a method when a thread is blocked, interrupt() although it does not break a running thread, but it can cause a blocked thread to throw an interrupt exception, which causes the thread to end the blocking state prematurely. Exit the clogging code.
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)
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)?
4.2 What is the difference between a synchronous code block and a synchronization method?
- The synchronization method is directly preceded by the method name, as in 3.1
synchronized , while the synchronous code block adds the keyword inside the method synchronized .
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?
- Mutually exclusive access is achieved by locking the shared resource.
- Thread state change: When a thread starts running, we give it a lock on the method it accesses, at which point the other thread cannot access the method, and when the current thread finishes executing, unlocks it, the second thread begins access (at which point other threads cannot access the method), and so on until all threads stop running.
4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?
- Use
wait() , notify() methods to implement the collaborative work of the thread.
5 Collaboration between Threads: 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?
- Not normal. When there are goods in the warehouse, the results of operation show no goods. This is not the same as the speed of adding and out of the library.
5.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
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.1. Code Cloud codes Submission record
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
201621123006 Java programming 11th Week of study summary