Java Multithread
Hi All. I am trying to make a program in Java that uses Multithread. That program is about a bank account shared between husband and wife who use debit cards.
I wrote the Java program using 2 threads but i am running through java.lang.NullPointerException.
I don't know my mistake, if someone will help I will appreciate it.
The underscore has been replaced here by _ Sorry about the inconvenience.
Thank you.
Here the program :
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
// A bank account has a balance that can be changed by
deposits and withdrawals.
public class BankAccount {
public double total = 0, amount = 0;
public void withdraw(double amount) {
total -= amount;
System.out.println("Amount Withdrawn is " + amount);
}
public void deposit(double amount) {
total += amount;
System.out.println("Amount Deposited is " + amount);
}
public double getAccount() {
System.out.println("Total Amount is " + total);
return total;
}
}
class WithdrawalRunnable {
private static final Lock lock = new ReentrantLock();
final Condition myCond = lock.newCondition();
BankAccount myAccount;
public void amountWithdrawn(double money_Withdrawn) throws InterruptedException {
lock.lock();
try {
while (money_Withdrawn > myAccount.total) {
myCond.await();
//when the condition is satisfied then :
myAccount.withdraw(money_Withdrawn);
myAccount.getAccount();
}
} finally {
lock.unlock();
}
}
}
class DepositRunnable {
private static final Lock lock = new ReentrantLock();
final Condition myCond = lock.newCondition();
BankAccount myAccount;
public void amountDeposited(double money_deposited) throws InterruptedException {
lock.lock();
try {
myAccount.deposit(money_deposited);
myAccount.getAccount();
myCond.signalAll();
} finally {
lock.unlock();
}
}
}
class Husband implements Runnable {
DepositRunnable myDeposit;
WithdrawalRunnable myWithdrawal;
double amount_deposit;
double amount_withdraw;
public Husband(double amount_deposit, double amount_withdraw) {
this.amount_deposit = amount_deposit;
this.amount_withdraw = amount_withdraw;
}
public void run() {
try {
myDeposit.amountDeposited(amount_deposit);
myWithdrawal.amountWithdrawn(amount_withdraw);
} catch (InterruptedException ex) {
Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class Wife implements Runnable {
DepositRunnable myDeposit;
WithdrawalRunnable myWithdrawal;
double amount_deposit;
double amount_withdraw;
public Wife(double amount_deposit, double amount_withdraw) {
this.amount_deposit = amount_deposit;
this.amount_withdraw = amount_withdraw;
}
public void run() {
try {
myDeposit.amountDeposited(amount_deposit);
myWithdrawal.amountWithdrawn(amount_withdraw);
} catch (InterruptedException ex) {
Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class RunningThreadTest {
public static void main(String[] args) throws InterruptedException {
Husband husb = new Husband(100.0, 0.0);
Wife wif = new Wife(400.0, 0.0);
Thread thread1 = new Thread(husb);
Thread thread2 = new Thread(wif);
thread1.start();
thread2.start();
}
}
View Answers
Related Tutorials/Questions & Answers:
Java MultithreadJava Multithread Hi All. I am trying to make a program in
Java that uses
Multithread. That program is about a bank account shared between husband and wife who use debit cards.
I wrote the
Java program using 2 threads but i am
Java MultiThread - Java BeginnersJava MultiThread what is the meaning of standalone application in multithread.How can we write multi programms in
multithread process and execution....");
}
}
-----------------------------------------------------
Visit for more information.
http://www.roseindia.net/
java/thread/
Java Advertisements
java multithread - Java Beginnersjava multithread Hi,
Thanks for your multithreading
java code. It helped me a lot.
But i have one issue. My code is look like this.
public class classname extends Thread {
public classname() {
super
java multithread - Java Beginnersjava multithread Hi,
I have developed one
java application.It should be running continuously. Using multithreads how to make the application to run continuously.
Could you please help me.
Thank you, Hi Friend
java multithread - Development processjava multithread sir i am send it again sir.please reply me sir.Develop multi-threaded echo server and a corresponding GUI client in
java. Multi-threaded ECHO SERVER and GUI client program by Whitman Kumaresh for my
Java MultiThread
Java has multithreading feature.
Multithreading feature is given by Thread class and Runnable
interface
Java Multithreading allows to run multiple tasks(threads) at a time.
Java Multi Thread Example
public class multi
Java Interview Questions - Page 5
Java Interview Questions - Page 5
Question: How to create
multithread in a program... to start the thread.
Question: Can
Java object be locked down for exclusive
Multithreading ? - Java Interview QuestionsMultithreading ?
Hi Friends,
I am new to
java , am not clear with Multithreading.
Multithread will cause deadlock. We can... program.
For read more information on
Multithread visit to :
http
Java Exception Thread Java Exception Thread
... the program. Many
Thread run concurrently in the program.
Multithread are those...,the thread run
concurently,synchronous or asynchronous.
Advantage of
Multithread Java Thread In Applet
Java Thread Applet is a
java class that runs inside the internet browser.
It is used to make the gui application, network application in
java
Thread is used in applet to make the
multithread application
Example of
Java Thread
Java Get Example
Java Get Example
In this section of
Java Examples, we are going to illustrate various
Java..... If
you are a
Java beginner this is a best place for you to learn different
javajava diff bt core
java and
java JAVAJAVA how the name came for
java language as "
JAVA java java why iterator in
java if we for loop
javajava explain technologies are used in
java now days and structure
java javajava different between
java & core
java Java Java Whether
Java is pure object oriented Language
javajava is
java open source
javajava what is
java reflection
java java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
javawhat is the size of array in
java ? what is the size of array in
java ?
what is the mean of finalize in
java javajava give a simple example for inheritance in
java javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava why to set classpath in
java java java What is ?static? keyword
javajava RARP implementation using
java socket
javajava sample code for RARP using
java javajava Does
java allows multiline comments
JavaJava how to do
java in command prompt
javajava Write a
java code to print "ABABBABCABABBA
javajava write a program in
java to acess the email
javajava send me
java interview questions
javajava how use
java method
javajava what are
JAVA applications development tools
JavaJava Whether
Java is Programming Language or it is SOftware
javajava is
java purely object oriented language
javajava why multiple inheritance is not possible in
java javajava explain object oriented concept in
java java java difference between class and interface
JavaJava how to draw class diagrams in
java javajava write a
java program using filenotfoundexception