Example #1
0
  public void consumir(Thread thr) throws InterruptedException {

    vacio.acquire();
    mutex.acquire();

    buffer[salida] = 0;
    System.out.println("Hebraconsumidora: " + thr.getName() + " consume en posiciĆ³n: " + salida);
    Productorconsumidor.mostrar.setText(
        Productorconsumidor.mostrar.getText() + "Productor consume " + "\n");
    salida = (salida + 1) % buffer.length;
    contador = contador - 1;
    System.out.println("Consumidor consume ");
    for (int i = 0; i < buffer.length; i++) {
      System.out.print("[" + buffer[i] + "]");
    }

    for (int i = 0; i < buffer.length; i++) {
      Productorconsumidor.mostrar.setText(
          Productorconsumidor.mostrar.getText() + "[" + buffer[i] + "]");
    }
    Productorconsumidor.mostrar.setText(Productorconsumidor.mostrar.getText() + "\n");
    System.out.print("\n");

    mutex.release();
    lleno.release();
  }
Example #2
0
  public void producir(Thread thr) throws InterruptedException {

    lleno.acquire();
    mutex.acquire();

    buffer[entrada] = 1;
    System.out.println("Hebraproductora: " + thr.getName() + " produce en posiciĆ³n: " + entrada);
    Productorconsumidor.mostrar.setText(
        Productorconsumidor.mostrar.getText() + "Productor produce " + "\n");
    entrada = (entrada + 1) % buffer.length;
    contador = contador + 1;

    System.out.println("Productor produce ");
    for (int i = 0; i < buffer.length; i++) {
      System.out.print("[" + buffer[i] + "]");
    }

    for (int i = 0; i < buffer.length; i++) {
      Productorconsumidor.mostrar.setText(
          Productorconsumidor.mostrar.getText() + "[" + buffer[i] + "]");
    }
    Productorconsumidor.mostrar.setText(Productorconsumidor.mostrar.getText() + "\n");
    System.out.print("\n");

    mutex.release();
    vacio.release();
  }
Example #3
0
 @Test
 public void testThreadInterupt() {
   Thread thread =
       new Thread("interupt") {
         public void run() {
           for (; ; ) {
             try {
               foo();
               System.out.println(1);
             } catch (InterruptedException e) {
               e.printStackTrace();
               break;
             } catch (Exception e) {
               e.printStackTrace();
             }
             //                    if(Thread.interrupted()){
             //                        System.out.println("iiiiiii");
             //                        break;
             //                    }
           }
         }
       };
   thread.start();
   System.out.println(thread.getName());
 }
 public static void printThreads() {
   Thread[] threads = getAllThreads();
   for (int i = 0; i < threads.length; i++) {
     Thread t = threads[i];
     if (t != null) {
       System.out.println(t.getName());
     }
   }
 }
    @Override
    public void run() {
      byte[] buf = new byte[100];
      try {
        int len;
        while ((len = in.read(buf)) > 0) {
          String output = new String(buf, 0, len);
          Thread t = Thread.currentThread();
          System.out.println(
              "thread " + t.getName() + " " + t.getId() + ", read " + len + " bytes: " + output);
        }

      } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to read", e);

      } finally {
        try {
          in.close();
        } catch (IOException e) {
          logger.log(Level.SEVERE, "Failed to close", e);
        }
      }
    }