pablosotol

HilosInterfaz

Aug 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1.  
  2. package hilos;
  3.  
  4. import java.awt.FlowLayout;
  5. import javax.swing.JFrame;
  6. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  7. import javax.swing.JPanel;
  8. import javax.swing.JTextField;
  9.  
  10.  
  11.  
  12. public class Hilos extends JFrame{
  13.    
  14.    
  15.     private JTextField jf,jd;
  16.    
  17.    
  18.     public Hilos(){
  19.        
  20.         setLayout(new FlowLayout(FlowLayout.CENTER));
  21.         jf=new JTextField(50);
  22.         jd=new JTextField(50);
  23.         add(jf);
  24.         add(jd);
  25.         CCorriente cc=new CCorriente();
  26.         RunnableThreadGiro rtg=new RunnableThreadGiro(cc);
  27.         RunnableThreadDeposito rtd=new RunnableThreadDeposito(cc);
  28.         Thread t1=new Thread(rtg);
  29.         Thread t2=new Thread(rtd);
  30.            
  31.         t1.start();
  32.         t2.start();
  33.            
  34.            try{
  35.                t1.join();
  36.                t2.join();
  37.            }catch(InterruptedException ex){
  38.                
  39.            }
  40.            
  41.         setVisible(true);
  42.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  43.         setSize(600,600);
  44.        
  45.        
  46.     }
  47.    
  48.    
  49. public class RunnableThreadDeposito extends JPanel implements Runnable{
  50.      
  51.        private final CCorriente cc;
  52.        
  53.        public RunnableThreadDeposito(CCorriente cc){
  54.            this.cc=cc;
  55.        }
  56.        
  57.        @Override
  58.        public void run(){
  59.            
  60.            while(true){
  61.                try{
  62.                    Thread.sleep((long) (1 + (int) (Math.random() * 5000)));
  63.                    cc.deposito((int) (Math.random() * 10000));
  64.                  
  65.                    
  66.                    
  67.                }catch(InterruptedException ex){
  68.                    
  69.                }
  70.            }
  71.        }
  72. }
  73.  
  74. public class RunnableThreadGiro implements Runnable{
  75.        
  76.        private final CCorriente cc;
  77.      
  78.        public RunnableThreadGiro(CCorriente cc){
  79.              
  80.            this.cc=cc;
  81.            
  82.        }
  83.        
  84.        @Override
  85.        public void run(){
  86.            
  87.               while(true){
  88.                   try{
  89.                       Thread.sleep((long) (1+ (int) (Math.random() * 5000)));
  90.                       cc.giro((int) (Math.random() * 10000));
  91.                      
  92.                   }catch(InterruptedException ex){
  93.                      
  94.                   }
  95.               }
  96.        }
  97.    
  98. }
  99.  
  100.  
  101. public class CCorriente{
  102.    
  103.        private final int MAX_SALDO=100000;
  104.        private int saldo=0;
  105.    
  106.        public synchronized void deposito(int dinero){
  107.               try{
  108.                   while(saldo>=MAX_SALDO){
  109.                   wait();  
  110.                   }
  111.                   saldo+=dinero;
  112.                   jf.setText(" Deposito: " +dinero + " Saldo: " +getSaldo());
  113.                   setVisible(true);
  114.                   notifyAll();
  115.              
  116.               }catch(InterruptedException ex){
  117.                  
  118.               }
  119.        }
  120.        
  121.        public synchronized void giro(int dinero){
  122.               try{
  123.                   while(saldo<=0){
  124.                       wait();
  125.                   }
  126.                   saldo-=dinero;
  127.                   jd.setText(" Giro: " +dinero + " Saldo: " +getSaldo());
  128.                   setVisible(true);
  129.                   notifyAll();
  130.              
  131.               }catch(InterruptedException ex){
  132.                  
  133.               }
  134.        }
  135.        
  136.        private int getSaldo(){
  137.               return saldo;
  138.        }
  139.    
  140. }
  141.  
  142.  
  143.    
  144.     public static void main(String[] args) {
  145.            
  146.            Hilos h=new Hilos();
  147.            
  148.            
  149.     }
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment