201621123078 "Java Programming" 11th Week study Total I knot

Source: Internet
Author: User
Tags mutex thread class

1. Study summary 1.1 This week summarize multithreading related content in the way you like (mind map or other).

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 Answer:ballrunnable class implements the class Runnable interface, supports multi-threaded operations, calls Thread.Sleep to hibernate in order to delay the completion of the thread time, let other threads run, if not, the program ends quickly, will not see the ball's running trajectory.

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?

Answer:

Q1:

    • A function move () that defines the movement of the ball;
    • The size of the ball and the coordinate function getshape () are defined.

Q2:

    • The Ballcomponent object is used to add a new ball and draw it during the program's run.
    • The internal ArrayList is used to store the small balls we see.
    • Only one Ballcomponent object was generated
    • Yes, in the run panel, a new thread is generated every time you click Start.
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?

Answer: In Ball.java, the Getshape () function changes the X,y,dx,dy to randomly generated:

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, for example, some can take the cosine trajectory, some can go square wave trajectory, some go Brownian motion, some go pentagram, and so on.

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?

Answer: Because of the Java single Inheritance mechanism, you inherit the thread class, then this custom thread class can no longer inherit other classes, and the interface may be implemented more than one, so by implementing the Runnable interface to achieve, Then this implementation class can inherit other classes as well.

b) 6-1,6-3,6-11 experimental summary.

2.2 Using a lambda expression to overwrite 6-3

Answer:

2.3 Title: 6-2 (runnable and stop thread). Answer: How do I need to properly stop a running thread?

Answer:
In this problem, we need to write a stopMe() method to stop the terminating thread, primarily by defining a flag,true when exiting normally. Stopping a thread can use the Thread.stop () method, but it is best not to use it. Although it can actually stop a running thread, this method is unsafe and is a deprecated method. We can use a Boolean variable tag value to stop the thread in the while loop.

Reference Blog: https://www.cnblogs.com/greta/p/5624839.html

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

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)?

Answer: You can use the Synchronized keyword to synchronize code blocks for mutually exclusive synchronous access.

4.2 What is the difference between a synchronous code block and a synchronization method?

Answer:

    • Synchronization method:
      That is, there are methods for synchronized keyword modification. Because each object in Java has a built-in lock, the built-in lock protects the entire method when the method is decorated with this keyword. You need to get the built-in lock before calling the method, otherwise it will be in a blocking state.
    • Synchronizing code blocks:
      That is, a statement block that has the synchronized keyword modifier. The block of statements decorated with this keyword is automatically added with the built-in lock for synchronization.

In comparison, the synchronization method has a larger scope than the synchronous code block.

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?

Answer: The use of critical resources between processes is mutually exclusive, it is called mutual exclusion, the principle is that when a resource is occupied by a task, the lock on it, other tasks can not access it until the resource is unlocked, is unlocked, there will be another task to use the resource, and so on.

4.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together?

In Java, you can use the Wait () and notify ()/notifyall () methods to reconcile the running progress (read) relationships between 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?

Answer:


The result is not normal, the remaining goods will exist in the case of not 0. Because there is only a mutex between add and remove, there is no synchronization, and the access speed varies, resulting in an error.

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 system of the functional modules table, the table reflects the owner of each module.
member responsible for module
Lu Weibin Borrowing books, viewing borrowed books
Yu Yuming View personal information, browse books
Su Zhiying Return books, Time passes
6.2 Run the video 6.3 Explain the module you are responsible for, and paste the key code of your own module (the number and name appears).

I am responsible for borrowing books and viewing the parts of books that have been borrowed.
Borrowing books:

View book Status:

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
Week Time Total code Amount New Code Volume total number of files number of new files
1 0 0 0 0
2 280 280 5 5
3 530 250 9 4
4 950 420 15 6
5 1345 395 20 5
6 1924 579 26 6
7 2248 324 31 5
8 2677 429 36 5
9 3020 343 42 6
10 3349 329 48 6
11 3725 376 54 6

201621123078 "Java Programming" 11th Week study Total I knot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.