Week11 "Java Programming" job summary

Source: Internet
Author: User
Tags set time thread class

Week11 "Java Programming" job Summary 1. This week's study summary 1.1 summarizes multithreading-related content in the way you like (mind map or other).

For:

2. Written work

Multi-thread of this PTA job title set

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?

A: Implement the Runnable interface, which represents the task. In the task, redefine the Run method, and the process task code that will be performed is written to run (). You can assign tasks to threads through the Ballrunnable class and support multithreading.
Call Thread.Sleep to Hibernate, that is, to let the currently running thread discard its own CPU), to the other threads to run the opportunity, so that the current thread to suspend the role.

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:

    • Ball.java did two things, 1, Move () method: Move the ball to the next position, when the ball encountered the edge, the opposite direction to move the ball. 2, Getshape (): The ball in the current position to obtain the shape of the ball.
    • The Ballcomponent object is added (ball b) To add small balls, and paintcomponent (Graphics g) is two operations, namely adding a small ball to the panel and drawing a small ball. The internal ArrayList is used to store small balls.
    • A Ballcomponent object. With multithreading, a thread starts every start, so the ball is drawn in different threads.
      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, 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? b) 6-1,6-3,6-11 experimental summary.
For:

    • The Operation Runnable interface is more resilient, giving classes the opportunity to inherit other classes and avoid the limitations of single inheritance. Facilitates the process of sharing resources when multiple threads of the same code are shared. Multiple threads share the code in the implementation class, and the code is simple and efficient.
    • 6-1: Learn how to create threads and assign tasks to threads. The task class can inherit the thread class, and the task code is in the replication Run method.
      6-3: When the task code is simpler, you can use anonymous inner classes and lambda expressions to rewrite the task code.
      6-11: The task class implements the Runnable interface, which writes the task code to the subclass run method that implements the interface.

      2.2 Using a lambda expression to overwrite 6-3

      For:

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

      A: The way to stop a thread is:
      1, the thread itself calls the Stop () method, self-terminates.
      2. Terminating a thread using the interrupt method
      3. Use the Boolean flag to exit the run () method in the thread

3. Mutex Access 3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number)

A: The method of counter will be modified with synchronized. Each thread is mutually exclusive access to the ID of the counter so that the ID end result is 0.

After running:

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:
1. Use synchronized to synchronize the program block. Lock the object that you want to manipulate (this), who gets the lock, and who can run the code that controls the object (this). With an object as a lock, you can manipulate that piece of code that the object controls.

public  void deposit(int money){    synchronized(this){        this.balance+=money;    }}public void withdraw(int money){    synchronized(this){        this.balance-=money;    }

2. Reentrantlock,lock,unlock method

public class Account {    private Lock poolLock = new ReentrantLock();     int balance;    public  void deposit(int money){        poolLock.lock();        this.balance+=money;        poolLock.unlock();    }    public void withdraw(int money){        poolLock.lock();        this.balance-=money;        poolLock.unlock();    }}
4.2 What is the difference between a synchronous code block and a synchronization method?

For:
1, synchronous method directly on the method plus synchronized implementation lock, synchronous code block inside the method lock, synchronization method lock range is larger, and the synchronization code block range to smaller point.
2, the use of synchronous method, then the entire method of all content will be treated as an atomic operation. In fact, however, when we may only have one section of the method that needs to be synchronized, synchronizing blocks of code can help us synchronize only where necessary.
3, in the synchronization code block can be free to select the lock. In the synchronous code block, we are free to select any Java object instance as the lock to be used during the synchronization process. However, for instance synchronization methods, this lock is not selectable, this lock can only be the object instance.

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?

A: Get a unique lock to open a resource without allowing the other thread's task to disturb, the other thread waits, notify () enters lock Pool, and when the resource is used, the lock is returned
, which is Poollock.unlock (), at which point the thread in lock pool can continue.

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

A: Use synchronized, wait, notify and other keywords and methods to achieve thread collaboration.

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: not normal. When the warehouse is full, the operation to add the goods is not carried out, but there are still counts, and the last remaining goods are not 0.

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 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 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 0 0 0 0
4 0 0 0 0
5 739 739 16 16
6 1084 345 28 12
7 1180 96 30 2
8 1627 447 35 5
9 1986 359 44 9
10 2350 364 56 12
11 2534 184 61 5
12 2909 375 70 9

Week11 "Java Programming" job 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.