pablosotol

HilosInterfaz

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