Advertisement
xickoh

Exer2

Nov 21st, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. import java.io.BufferedWriter;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.util.concurrent.*;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14.  *
  15.  * @author francisco
  16.  */
  17. public class exer2 implements Runnable {
  18.  
  19.     String filename;
  20.     Semaphore s;
  21.     Semaphore s2;
  22.     SharedCounter share;
  23.  
  24.     public exer2(Semaphore sem, Semaphore sem2, String filename, SharedCounter counter) {
  25.         s = sem;
  26.         s2 = sem2;
  27.         this.filename = filename + ".txt";
  28.         share = counter;
  29.  
  30.     }
  31.  
  32.     public void writeOnFile(int num) {
  33.         int counter = share.getCounter();
  34.         if (counter <= 10) {
  35.             try {
  36.                 String text = ("Thread " + Thread.currentThread().getName() + " escreveu " + num + "x. Counter: " + counter + "\n");
  37.                 BufferedWriter writer = new BufferedWriter(new FileWriter(this.filename, true));
  38.                 writer.append(text);
  39.                 writer.close();
  40.                 System.out.println(text);
  41.                 Thread.sleep(1000);
  42.             } catch (IOException ex) {
  43.  
  44.             } catch (InterruptedException ex) {
  45.             }
  46.         }
  47.     }
  48.  
  49.     public void run() {
  50.         System.out.println("Thread " + Thread.currentThread().getName() + " iniciou");
  51.  
  52.         try {
  53.             s.acquire();
  54.             share.incrementCounter();
  55.             writeOnFile(1);
  56.             s.release();
  57.  
  58.         } catch (InterruptedException ex) {
  59.  
  60.         }
  61.  
  62.         try {
  63.             Thread.sleep(1000);
  64.         } catch (InterruptedException ex) {
  65.  
  66.         }
  67.  
  68.         try {
  69.             s2.acquire();
  70.             share.incrementCounter();
  71.             writeOnFile(2);
  72.             s2.release();
  73.         } catch (InterruptedException ex) {
  74.  
  75.         }
  76.  
  77.     }
  78.  
  79.     public static void main(String args[]) {
  80.         String filename = args[0];
  81.         SharedCounter share = new SharedCounter();
  82.         Semaphore sem = new Semaphore(1);
  83.         Semaphore sem2 = new Semaphore(1);
  84.  
  85.         for (int i = 0; i < 20; i++) {
  86.  
  87.             Thread th = new Thread(new exer2(sem, sem2, filename, share), "Th" + i);
  88.             th.start();
  89.         }
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement