Thread Synchronization and ([Supplement] timer and timertask)

Source: Internet
Author: User

13.4 Thread Synchronization

1. Cause of thread synchronization: solve the "access conflict" problem of data.

2. Critical resource: the data shared between multiple threads is called a critical resource.

I. mutex lock

1. An object is marked with a mutex lock. Only one thread can access the object at a time.

2. The key word synchronized is used to associate with the object's mutex lock.

3. When an object is modified with synchronized, it indicates that the object has started the "mutex lock" mechanism and can only be accessed by one thread at any time, even if the thread is blocked, the lock status of this object will not be released, and other threads cannot access this object.

4. How to Use synchronized

(1) SynchronizationCodeBlock: used to limit the execution of a piece of code before an object

Public void push (char c ){
...
Sychronized (this ){
Data [Index] = C;
Index ++
}
}

(2) synchronous method: Used in method declaration

A synchronization benefits: thread security issues

Disadvantages of B Synchronization

Reduced operation efficiency (determined that the lock is resource-consuming)

Synchronization nesting, prone to deadlocks,

 

5 deadlock

(1) cause: There are two AB threads, and B is used in a running. AB Shares S (shared resources). A runs first. After running, only one thread can be accessed because s is locked. B waits for S to run and a waits for B to run. Both threads wait for each other and cannot run.

(2) In order to avoid deadlocks, it is necessary to allow the thread to release its locked resources as much as possible when it enters the blocking state, to provide other threads with the opportunity to run. Available Methods:

A. wait (): A locked object can call the wait () method. This will cause the current thread to be blocked and release the mutex lock of the object, that is, the wait () is removed () method to lock the current object. Other threads have the opportunity to access this object.

B. Wake y (): Wake up the thread that is blocked after the wait () method is called.

C. policyall (): Wake up all threads that are blocked by calling the wait () method.

 

(Fill) Timer: timer and timertask

1. The Timer class implements a function similar to the alarm clock, that is, to trigger a thread at a scheduled time or at a certain interval.

2. Create a subclass by inheriting the timertask class, so that the subclass can achieve multithreading. write the code that requires multi-thread execution inside the run method, and then start thread execution through the Timer class.

3. Schedule () method

(1) Public void schedule (timertask task, Date Time): This method is used to execute a thread task when it reaches the specified time or exceeds the specified time.

(2) Public void schedule (timertask task, date firsttime, long period): starts the specified thread of the task every milliseconds after the time reaches firsttime, in this way, the thread will be started again.

(3) Public void schedule (timertask task, long delay) starts the thread task after executing the schedule method delay in milliseconds

(4) Public void schedule (timertask task, long delay, long period): After the schedule method delay is executed, the thread task is started, and then the thread task is started again every period millisecond.

 

 

Import java. util .*;

Public class texttimer {

Public static void main (string [] ARGs ){

Timer T = new timer ();

Mytimertask task = new mytimertask ();

// T. Schedule (task, 500); // run within 500 milliseconds

// T. Schedule (task, new date (2011-1900,9-, 20); // run at a certain time

T. Schedule (task, 0,500); // run every 500 milliseconds

For (INT I = 0; I <10; I ++ ){

System.Out. Println ("main thread" + I );}}}

 

Only for personal favorites reproduced from: http://blog.csdn.net/mayouarebest8621/article/details/6755036

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.