Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prueba;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- public class Prueba extends JFrame implements ActionListener{
- JButton jp=new JButton("Inicio");
- JTextField jf=new JTextField(50);
- JTextField jd=new JTextField(50);
- private HebraDeposito h;
- private HebraGiro g;
- public Prueba(){
- setLayout(new FlowLayout());
- add(jf);
- add(jd);
- jp.addActionListener(this);
- add(jp);
- setSize(600,600);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==jp){
- h=new HebraDeposito(this);
- h.start();
- g=new HebraGiro(this);
- g.start();
- }
- }
- private final int MAX_SALDO=100000;
- private int saldo=0;
- public synchronized void deposito(int dinero){
- try{
- while(saldo>=MAX_SALDO){
- wait();
- }
- saldo+=dinero;
- jf.setText(" Deposito: " +dinero + " Saldo: " +getSaldo());
- notifyAll();
- }catch(InterruptedException ex){
- }
- }
- public synchronized void giro(int dinero){
- try{
- while(saldo<=0){
- wait();
- }
- saldo-=dinero;
- jd.setText(" Giro: " +dinero + " Saldo: " +getSaldo());
- notifyAll();
- }catch(InterruptedException ex){
- }
- }
- public int getSaldo(){
- return saldo;
- }
- public static void main(String[] args) {
- new Prueba();
- }
- }
- package prueba;
- public class HebraDeposito extends Thread{
- Prueba p;
- public HebraDeposito(Prueba p){
- this.p=p;
- }
- @Override
- public void run(){
- while(true){
- try{
- Thread.sleep((long)(1 + (int)(Math.random() * 5))*1000);
- p.deposito((int) (Math.random() * 10000));
- }catch(InterruptedException ex){
- }
- }
- }
- }
- package prueba;
- public class HebraGiro extends Thread {
- Prueba p;
- public HebraGiro(Prueba p){
- this.p=p;
- }
- @Override
- public void run(){
- while(true){
- try{
- Thread.sleep((long) (1 + (int) (Math.random() * 5)) *1000);
- p.giro((int) (Math.random() * 10000));
- }catch(InterruptedException ex){
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment