Java multithreaded synchronization synchronized keyword __java

Source: Internet
Author: User

Synchronized keyword: When the synchronized keyword modifies a method, this method is called the synchronization method.



Each object in Java has a lock, or a monitor, that represents the synchronized method of an object when it accesses the

Lock the object so that no other thread can go back to the synchronized method, knowing that the previous thread

When the method completes (or throws an exception), the lock of the object is freed and other threads are likely to access the Synchronized method again.

Instance Code Demo:

public class Fetchmoney
{
public static void Main (string[] args)
{
Bank Bank = new Bank ();

Thread T1 = new Moneythread (bank);
Thread t2 = new Moneythread (bank);

T1.start ();
T2.start ();
}
}
Class Bank
{
private int money = 1000;

public synchronized int Getmoney (int number)
{
if (number < 0)
{
return-1;
}

else if (number > Money)
{
Return-2;
}

Else
{
Try
{
Thread.Sleep (1000);
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}

Money = number;

return number;
}
}
}


Class Moneythread extends Thread
{
Private Bank bank;

Public Moneythread (Bank bank)
{
This.bank = Bank;
}

@Override
public void Run ()
{
System.out.println (Bank.getmoney (800));
}
}



It is worth emphasizing that the Synchronized keyword lock is the object, not the object's method


If a synchronized method is static, then when the thread accesses the method, the

He's not locking the object that synchronized method locks on. But

The class object corresponding to the object that the synchronized method locks on.

Because no matter how many objects a class has in Java, these objects correspond to only one class object,

So when a thread accesses the two static,synchronized methods of the two objects of the same class separately, they perform the order,

This means that one thread executes the method first, and the execution is completed before another thread starts executing.



Synchronized code block simple Code instance fragment

Class Test

{

Object obj = new Object ();

public void Execute ()

{

Synchronized (obj)

{

for (int i = 0; i < i++)

{

Try

{

Thread.Sleep ((Long) (MATH.RANDOM * 1000));

}

catch (Interruptedexeception e)

{

E.printstacktrace ();

}

}

}


public void Execute2 ()

{

Synchronized (obj)

{

for (int i = 0; i < i++)

{

Try

{

Thread.Sleep ((Long) (MATH.RANDOM * 1000));

}

catch (Interruptedexeception e)

{

E.printstacktrace ();

}

}

}

}

}


Synchronized code blocks simply set the code to sync a bit more fine-grained, synchronized directly to modify the method is too coarse-grained at one time only one thread can access the method




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.