Java uses thread to do a simple ATM deposit and withdrawal Instance code _java

Source: Internet
Author: User
Tags stub

Implements the Runnable interface. When a thread has inherited another class, it is only possible to create a thread with a method that implements the Runnable interface, and it is easy to maintain the consistency of the program style.

We know that a thread with life has the following five states:

Create status

After a thread is created with the new operator, the thread is simply an empty object, and the system does not allocate resources, saying that the thread is in the created state (new thread)

Operational status

When a thread is started with the start () method, the system assigns the required resource, except the CPU, to the thread to make the thread operational (Runnable)

Run in State

The Java running system selects a runnable thread by dispatching it to occupy the CPU and switch to the running state (Running). At this point, the system actually executes the thread's run () method.

Blocking state

A running thread enters the blocking state for some reason it cannot continue running (Blocked)

State of death

The end of the thread is the state of Death (Dead)

Nonsense not much to say, the following directly on the code:

Java code:

Copy Code code as follows:

Package PACK.JAVA.THREAD.ATM;
/**
* Account category;
* @author Administrator
*
*/
public class Account {

private String name; User name;
private int value; Account balances;

/**
* Deposit amount;
* @param monery
*/
public void putmonery (int monery) {
This.value = This.value + monery;
}

/**
* Withdraw the amount;
* @param monery
* @return amount;
*/
public int getmonery (int monery) {

Determine whether the account balance is greater than the money to be taken out;
if (This.value > Monery) {
This.value = This.value-monery;
}else{
Monery = This.value; When the balance of the account is not sufficient, the amount of all account balances is taken out.
this.value = 0;
}
Return the money taken out;
return monery;
}

/**
* Check the balance;
* @return return account balances;
*/
public int search () {
return this.value;
}

Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int GetValue () {
return value;
}
public void SetValue (int value) {
This.value = value;
}
}

Java code:

Copy Code code as follows:

Import java.io.Serializable;
/**
* Withdrawal thread category;
* @author Administrator
*
*/
public class Fetchmoney extends Thread implements serializable{

Private static final long serialversionuid = -5059599151558445815l;

Private account Account; Account object;
private int monery; Balance

Public Fetchmoney () {
Super ();
}

Public Fetchmoney (int monery,account account) {
This.account = account;
This.monery = Monery;
}

@Override
public void Run () {
TODO auto-generated Method Stub
int currmonery = Account.search (); Current balance;
Synchronized (account) {
try {
Sleep (5); The time of payment of withdrawals;
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Remove the balance;
int getmonery = Account.getmonery (monery);
System.out.println ("Honorable" +account.getname () + "User Hello!" + "The current balance is:" +currmonery + "Yuan." + "Remove the balance is:" +getmonery+ "Yuan." + "account balance is:" +account.search () + "Yuan");
}
}
}

Java code:

Copy Code code as follows:

/**
* Deposit thread category;
* @author Administrator
*
*/
public class Savemoney extends Thread implements serializable{

Private static final long serialversionuid = 3095120546560212724L;

Private account Account; Account object;
private int money; Amount

Public Savemoney () {
Super ();
}

Public Savemoney (int money,account account) {
This.account = account;
This.money = money;
}

@Override
public void Run () {
TODO auto-generated Method Stub
int currmonery = Account.search (); Inquire account balance;

Synchronizing objects, allowing only single-threaded operations;
Synchronized (account) {
try {
Sleep (5); When the inquiry, the time to send the fee;
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Account.putmonery (This.money); Insert amount;
Export deposit information;
System.out.println ("Honorable" +account.getname () + "User Hello!" + "The current balance is:" +currmonery + "Yuan." + "Deposit balance is:" +this.money+ "Yuan." + "account balance is:" +account.search () + "Yuan");
}
}
}

Java code:

Copy Code code as follows:

/**
* ATM test class;
* @author Administrator
*
*/
public class Threadatmdemo {

/**
* Main method;
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
Account Account = new account ();
Set user name;
Account.setname ("Zhouhaitao");
Initialize the balance;
Account.setvalue (0);

deposited in 100;
New Savemoney (100,account). Start ();

deposited in 200;
New Savemoney (account). Start ();

Remove 500;
New Fetchmoney (account). Start ();
}
}

The results of the program operation are as follows:

Dear Zhouhaitao User Hello! The current balance is: 0 yuan. Deposit balance is: 100 yuan. Account balance: 100 yuan
Dear Zhouhaitao User Hello! The current balance is: 0 yuan. Deposit balance is: 200 yuan. Account balance: 300 yuan
Dear Zhouhaitao User Hello! The current balance is: 0 yuan. The balance is: 300 yuan. Account Balance: 0 yuan

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.