Two special classes in Java multithreaded synchronization

Source: Internet
Author: User

The Java language has a built-in synchronized keyword for synchronizing multithreading, greatly facilitating the programming of multithreaded routines in Java. However, using the Synchronized keyword alone is not enough to meet all the requirements for synchronizing multithreading. As you know, synchronized can only synchronize methods or blocks of code, and if one of our applications needs to be synchronized across multiple methods, synchroinzed is not competent. In C + + There are many synchronization mechanisms, such as signal volume, mutex, pro-session area. In Java, this synchronization tool can be built at a higher level on the basis of synchronized language features to facilitate our use.

Currently, it is widely used in a Java-synchronized toolkit written by Doug Lea, where you can learn more about this package:

Http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

The toolkit has been as JSR166 is under JCP control and is about to be a formal part of JDK1.5. This article does not intend to dissect the toolkit in detail, but to introduce a variety of synchronization mechanisms, and give examples of such synchronization mechanisms, this is not an industry-level implementation. But it will refer to some snippets of code from the industrial-level implementations in this synchronization package of Doug Lea.

The account class in the previous article is also used in this example, but here we write a new ATM class to simulate ATMs, generate 10 ATM threads through a Atmtester class, and query, withdraw, and deposit operations on John's account. The account class makes some changes to fit the needs of this article:

Import Java.util.HashMap;
Import Java.util.Map;
Class Account
{
String name;
float amount;
Using a map to simulate persistent storage
Static MAP storage = new HashMap ();
Static
{
Storage.put ("John", New Float (1000.0f));
Storage.put ("Mike", New Float (800.0f));
}
Public account (String name)
{
System.out.println ("New account:" + name);
THIS.name = name;
This.amount = ((Float) storage.get (name)). Floatvalue ();
}
Public synchronized void deposit (float amt)
{
Float amount = ((float) storage.get (name)). Floatvalue ();
Storage.put (name, new Float (Amount + amt));
}
Public synchronized void Withdraw (float amt)
Throws Insufficientbalanceexception
{
Float amount = ((float) storage.get (name)). Floatvalue ();
if (Amount >= amt) Amount-= Amt;
else throw new insufficientbalanceexception ();
Storage.put (name, new Float (amount));
}
public float getbalance ()
{
Float amount = ((float) storage.get (name)). Floatvalue ();
return amount;
}
}

In the new account class, we use a hashmap to store the accounts information. Account is used by the ATM class after login through login:

public class ATM
{
  Account acc;
  //作为演示,省略了密码验证
  public boolean login(String name)
  {
   if (acc != null) throw new IllegalArgumentException("Already logged in!");
   acc = new Account(name);
   return true;
  }
  public void deposit(float amt)
  {
   acc.deposit(amt);
  }
  public void withdraw(float amt) throws InsufficientBalanceException
  {
   acc.withdraw(amt);
  }
  public float getBalance()
  {
   return acc.getBalance();
  }
  public void logout ()
  {
   acc = null;
  }
}

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.