201621123065 Java programming 11th Week of study summary

Source: Internet
Author: User

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?

Ballrunnable class implements runnable interface, support multithreading, call Thread.Sleep to sleep in the specified time not participate in the CPU competition, so that the thread sleep can see the motion trajectory of the ball, if the time is 0 is thread.sleep (0), Trigger the operating system to immediately restart the CPU competition will not see the 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?
    • Realize the moving method of the small ball, get the coordinates and size of the ball
    • The purpose of the Ballcomponent object is to add small balls and draw small balls
    • ArrayList for storing small balls
    • A Ballcomponent object was generated in the run
    • Yes, each ball is drawn separately in different threads.

      2. Experiment Summary: Title set (multithreading) 2.1 title: 6-1 (thread), 6-3 (runnable-anonymous inner Class). Answer: A) What is the benefit of implementing multithreading by defining the implementation class of the Runnable interface, rather than inheriting from the thread class for multi-threading? b) 6-1,6-3,6-11 experimental summary. The benefit of defining the Runniable interface for multithreading is more than inheriting the thread class:
    • Scenarios for multiple threads of the same code to handle the same resource
    • Avoid limitations due to the single inheritance of Java
    • Enhance the robustness of the program, the code can be shared by multiple threads, code and data when the Independent

6-1 Experimental Summary:
When you create a Mythread class object, specify the number of cycles N, and then create a task that outputs an integer from 0 to n-1.

6-3 Experimental Summary:
Create an object that represents the Runnable implementation class for the task, and write the required three lines of output in the task.

6-11 Experimental Summary:
The problem is similar to 6-1, which is to implement a runnable interface more.

2.2 Using a lambda expression to overwrite 6-3

Rewrite as follows:

Thread t1 new Thread (( ) - >{                System.out.println(mainThreadName);                System.out.println(Thread.currentThread().getName());                System.out.println(Arrays.toString(getClass().getInterfaces()));            });
2.3 Title: 6-2 (runnable and stop thread). Answer: A) How do I need to properly stop a running thread? b) 6-2 experimental summary.

A) You cannot use the STOP ()!!! The Stop () method has been deprecated, and a shared variable can be used by multiple threads that perform the same task as a signal to interrupt the execution of a thread, or by using the interrupt method, which terminates the thread when the thread is blocked.

b) public void Stopme (); and the public void Sendword (String Word) method writes the statement flag = true; and this.word = word; can be implemented, primarily the run () method, for Word contains alien output in a format, Be aware while(!flag) of adding statements to determine the execution or termination of a program.

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)?
public static void addId() {    synchronized (Counter.class) {        id++;    }}

Mutually exclusive synchronous access with synchronized synchronous code block

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

Synchronizing code blocks:
That is, a statement block that has the synchronized keyword modifier.
Statement blocks that are decorated with this keyword are automatically added with built-in locks for synchronization
Here's how:

synchronized(object){     }

Synchronization method:
That is, there are methods for synchronized keyword modification.
Because every object in Java has a built-in lock, when the method is modified with this keyword,
Built-in locks protect the entire method. You need to get the built-in lock before calling the method, otherwise it will be in a blocking state.
Method:

public synchronized void save(){}
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: Object lock, in the program, each object has a lock, only to obtain the object lock, in order to execute the corresponding synchronized code block or method, the shared resources in a time period to allow only one thread access, finished access to another thread to access, Use the Synchronized keyword code as follows;

class Counter {    private static int id = 0;    public synchronized static void addId() {        id++;    }    public synchronized static void subtractId() {        id--;    }    public static int getId() {        return id;    }}

A thread competes for an object lock, while the rest of the thread waits for the thread to release the object lock while the threads release the object lock and the remaining threads start competing for the object lock until the program ends

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

Use the Wait (), notify (), notifyall () keyword to implement communication 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?


Two times put out the same, but the result is not the same, because the producer and consumer access speed is inconsistent, there is no cooperation between the threads will cause confusion

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. 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). 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 number Number of rows number of new rows Number of files number of new files
First week 39 39 9 9
Second week 278 239 18 9
Third week 431 153 33 15
Week Four 894 182 51 9
Week Five 1154 260 67 16
Week Six 175° 200 79 12
Seventh Week 1463 109 85 6
Eighth Week 1776 313 101 16
Nineth Week 1903 127 106 5
Tenth Week 2136 233 122 16
11th Week 2861 725 146 24

201621123065 Java programming 11th Week of study summary

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.