1. Summary of this week's study
2. Written assignment 1. Source code reading: What is the use of multi-threaded BounceThread1.1 ballrunnable classes? Why do I need to call Thread.Sleep to hibernate in my code?
- The ballrunable is designed to implement the Runable interface and to prepare the boot thread.
- Thread.Sleep sleep is to let us see the course of the ball movement, give us a visual pause, we can see the ball in motion.
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.java achieve the ball size and shape of the two things.
- The Ballcomponent object is considered a shape to make the ball.
- A ball B:balls object was created.
- No, the ball is drawn in the same thread, but the thread uses and thread.sleep the function so that the thread sleeps and the ball can be drawn continuously.
2. Experimental Summary: Title set (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?
- The ability to avoid repeatedly creating thread objects.
b) 6-1,6-3,6-11 experimental summary.
- 6-1 This experiment uses the getname of the thread function and the IsAlive syntax to complete the subject.
- 6-3 This experiment uses the anonymous inner class to complete the Runable function and complete the subject.
- 6-11 This experiment I completed the run function by inheriting the thread.
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 method used in the subject is a loop to keep the thread running, jumping out of the loop when encountering "alien", the run function jumps out of the loop, the function outputs "stop" after the function jumps out of the loop, and then the process ends.
3. Mutex 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 4.1 in addition to using the synchronized adornment method for mutually exclusive synchronous access, what else can be used to implement mutually exclusive synchronous access, using the code description (please show the relevant code and number)?
- Mutually exclusive synchronous access can be made by synchronizing code blocks
4.2 What is the difference between a synchronous code block and a synchronization method?
- The action range is different, the synchronous method locks the object, while the synchronous code block locks its method.
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?
- Principle: When a thread is going to process a resource, the system assigns a lock to it, locks the resource, and only the thread holds the key to the resource lock processing.
- such as pta6-4 we are to balance the resource to be processed, when the program executes synchronized synchronous code block, the system will assign a lock, only the thread to process.
4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?
- Use keywords such as synchronized, wait/notify, and so on to work together on threads.
5. Collaboration between Threads: Producer consumer issues 5.1 run 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, put out 100 pieces of goods should result in 0 pieces of goods, but sometimes the result is 10 pieces of goods.
5.2 Use synchronized, wait, notify solve the problem (key code, need to appear number)
3. Code cloud and PTA3.1. Codes Cloud Code submission record
3.2 "Multi-threaded" PTA Submission List
3.3 Count the amount of code completed this week
||Total code Amount
||New Code Volume
||total number of files
||number of new files
201621123021 Java programming 11th Week of study summary