Advertisement
xickoh

Ficha 5 exer 2

Dec 12th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import java.util.concurrent.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. public class Janela implements Runnable {
  8.  
  9.     protected Monitor m;
  10.     protected int i;
  11.  
  12.     public Janela(Monitor m, int i) {
  13.         this.m = m;
  14.         this.i = i;
  15.     }
  16.  
  17.     public void run() {
  18.         String myname = Thread.currentThread().getName();
  19.         JFrame f = new JFrame(myname);
  20.         JLabel l = new JLabel("#");
  21.         f.add(l);
  22.         f.setSize(150, 200);
  23.         f.setLocation(i * 200, 100);
  24.         f.setVisible(true);
  25.         m.setThreadNumber(Integer.parseInt(myname));
  26.         m.espera();
  27.        
  28.  
  29.         for (int i = 0; i < 20; i++) {
  30.             try {
  31.                 Thread.sleep(100);
  32.             } catch (InterruptedException ie) {
  33.             }
  34.             l.setText("" + l.getText() + "#");
  35.         }
  36.         f.dispose();
  37.     }
  38.  
  39.     public static void main(String args[]) {
  40.         Monitor mon = new Monitor();
  41.         Thread[] ths = new Thread[8];
  42.  
  43.         for (int i = 0; i < ths.length; i++) {
  44.             ths[i] = new Thread(new Janela(mon, i), "Th" + i);
  45.             ths[i].start();
  46.         }
  47.  
  48.         System.out.println("[Main] All threads created!");
  49.         try {
  50.             Thread.sleep(5000);
  51.         } catch (InterruptedException ex) {
  52.         }
  53.         System.out.println("[Main] Activting threads!");
  54.  
  55.         for (int i = 0; i < ths.length; i++) {
  56.             mon.acorda(i);
  57.             try {
  58.                 Thread.sleep(1000);
  59.             } catch (InterruptedException ex) {
  60.             }
  61.         }
  62.  
  63.         try {
  64.             for (int i = 0; i < ths.length; i++) {
  65.                 ths[i].join();
  66.             }
  67.         } catch (InterruptedException ie) {
  68.         }
  69.         System.out.println("[Main] All threads ended!");
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement