Java multi-thread synchronization code block, java multi-thread synchronization

Source: Internet
Author: User

Java multi-thread synchronization code block, java multi-thread synchronization

/* Multi-thread security problems
1. Why is there a security problem?
When the program is running, a thread is qualified for execution after the judgment conditions are met, but no code is run.
The latter thread also judges the conditions and is qualified for execution. The latter thread runs the code, but at this time the conditions for running the thread are not met.
At this time, there is a security problem.

2. The method implementing the interface thread cannot throw an exception!

*/


/* Running status
Through analysis, it is found that the incorrect tickets such as 0,-1, and-2 are printed.

Security problems occur during multi-thread running.

Why?
When multiple statements operate on the data shared by the same thread, one thread only executes a portion of the statements.
Another thread is involved, leading to data sharing errors.

Solution
Statements that share data for multiple operations can only be executed by one thread. Other threads are not involved in execution.

Java provides a professional solution to multithreading security issues
Synchronous Code Block
Format:
Synchronized (object) objects can be arbitrary (use God)
{
Code to be synchronized (which code shares data)
}

The object is like a lock. The thread holding the lock can be executed in synchronization.
The thread that does not hold the lock does not get the execution right of the cpu, because it does not have the permission to get the execution right.

Prerequisites for synchronization:
1. You must have two or more
2. Multiple Threads must use the same lock.

Ensure that only one thread is running in synchronization.

Benefits: multi-thread security

Disadvantages: multiple threads need to judge the lock, which consumes more resources.
*/


Class Ticket implements Runnable // extends Thread
{
Object obj = new object ();
Private static int tick = 100;
Public void run ()/* the synchronous function cannot be written here. Otherwise, other threads cannot use the function. You need to write a synchronous function separately */
{
While (true)
{Synchronized (obj)
{
If (tick> 0)
{
Try
{
Thread. sleep (10);/* sleep after each running */
}
Catch (Exception e)
{

}
System. out. println (Thread. currentThread (). getName () + "sale: --" + tick --);
}
}
}
}
}

Class TicketDemo
{
Public static void main (String args [])
{
Ticket t = new Ticket ();
Thread t1 = new Thread (t );
Thread t2 = new Thread (t );
Thread t3 = new Thread (t );
Thread t4 = new Thread (t );

T1.start ();
T2.start ();
T3.start ();
T4.start ();
}
}

Related Article

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.