201521123105 11 Week Java Learning Summary

Source: Internet
Author: User

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

2. Written work this time PTA job title set multi-thread 1. Mutually exclusive access and synchronous access complete the set of 4-4 (mutually exclusive access) and 4-5 (synchronous access) 1.1 In addition to using the synchronized adornment method to achieve mutually exclusive synchronous access, what is the way to achieve mutually exclusive synchronous access (please show the relevant code)?

In Java, there are two ways to achieve synchronous mutex access: synchronized and lock

Class account{private int balance;    Private Lock Poollock = new Reentrantlock ();    Private Condition Condition = Poollock.newcondition ();        Public account (int balance) {super ();    This.balance = balance;    } public int GetBalance () {return balance;        } public void deposit (int money) {poollock.lock ();            try{this.balance=getbalance () + money;        Condition.signal ();        } finally {Poollock.unlock ();        }} public void withdraw (int. money) {Poollock.lock ();                try{while (GetBalance () <money) {try {condition.await ();                } catch (Interruptedexception e) {e.printstacktrace ();            }} this.balance=getbalance ()-money;        Condition.signal ();        } finally{Poollock.unlock (); }    }               }
1.2 What is the difference between a synchronous code block and a synchronization method?

The synchronization method is to add the Synchronized keyword before the program code of the atomic operation,

public static synchronized void Addld () {                id++;        }
A synchronous code block is a block of synchronized code that is added to the method, such as:
  public static void Addld () {                synchronized (counter.class) {//synchronized (syncobject), syncobject== lock Object                        id++;                }        }
Synchronized block one of the code must obtain a lock on the object SyncObject (which can be a class instance or Class) to execute.
1.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?

The principle of mutually exclusive access: When the resource provisioning task is used, it is locked on. If the task that you want to access a resource must lock the resource so that other tasks cannot access it until the resource is unlocked, the object that has no object lock can only enter the lock pool waiting for the thread to end, then compete with the other objects for the lock, have the lock to execute, and allow only one thread to use the resource at a time. Until all threads end.

1.4 What keywords are used in Java Multi-threading to implement communication between threads, so that threads work together? Why does synchronous access generally have to be put into synchronized methods or blocks of code?

The Wait (), notify () object can be used to collaborate between threads, as well as await (). Synchronous access is typically put to the synchronized method to prevent conflicts caused by multiple threads accessing the same resource.

2. Perform the experiment summary alternately (whether or not it is done)

To have a flag Boolean value to determine which block of code to execute. Because two threads are executed alternately, the threads need to collaborate with each other, so the wait,notify is used and the flag flag mechanism is implemented. The GetSize () method returns the number of tasks that are completed and ends the program when all tasks are completed.

3. Mutex Access 3.1 Modify the Testunsynchronizedthread.java source code so that it can be accessed synchronously. (Key code, need to appear number) 3.2 Further use of actuators to improve the corresponding code (key code, need to appear number) Reference: Java multi-thread executor, Executorservice, executors, callable, Future and FUTURETASK4. Inter-Threading collaboration: Producer consumer issues 4.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?

Run more than a few times to find that the warehouse left the goods are not 0:

The production speed of producers and the speed of the consumers are not consistent, there is no cooperation between the threads, there is confusion.

4.2 Use synchronized, wait, notify solve the problem (key code, need to appear number) 4.3 Choose to do: Use the lock with the condition object to solve the problem. 5. Query the information answer: What is thread safety? (Use your own words and code to summarize, write your own understanding of the work)

If the code is in a process where multiple threads are running concurrently, these threads may run the code concurrently. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe.

6. Select: Experiment Summary

201521123105 11 Week Java Learning 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.