Guest

chissà quanti errori XD

By: a guest on Jan 28th, 2012  |  syntax: Java  |  size: 7.59 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. class SpecialBarriera{
  2.        
  3.         private int numeroThreads, threadArrivati;
  4.         boolean nera;
  5.         ReentrantLock lock = new ReentrantLock();
  6.         Condition cond = lock.newCondition();
  7.        
  8.         public SpecialBarriera(int numeroThreads){
  9.                 this.numeroThreads = numeroThreads;
  10.                 this.threadArrivati = 0;
  11.                 this.nera = false;
  12.         }
  13.        
  14.         public void awaitT(){
  15.                 lock.lock();
  16.                 while(threadArrivati < numeroThreads)
  17.                         try {
  18.                                 cond.await();
  19.                         } catch (InterruptedException e) {
  20.                                 // TODO Auto-generated catch block
  21.                                 e.printStackTrace();
  22.                         }
  23.                
  24.                 lock.unlock();
  25.         }
  26.        
  27.         public void doneT(){
  28.                 lock.lock();
  29.                 ++threadArrivati;
  30.                 if(threadArrivati == numeroThreads)
  31.                         cond.signal();
  32.                 lock.unlock();
  33.         }
  34.        
  35.         public boolean awaitP(){
  36.                 lock.lock();
  37.  
  38.                 while(nera == false && threadArrivati < numeroThreads)
  39.                         try {
  40.                                 cond.await();
  41.                         } catch (InterruptedException e) {
  42.                                 // TODO Auto-generated catch block
  43.                                 e.printStackTrace();
  44.                         }
  45.                
  46.                 lock.unlock();
  47.                 return nera;
  48.         }
  49.        
  50.         public void reset(){
  51.                
  52.                 lock.lock();
  53.                 while(threadArrivati < numeroThreads)
  54.                         try {
  55.                                 cond.await();
  56.                         } catch (InterruptedException e) {
  57.                                 // TODO Auto-generated catch block
  58.                                 e.printStackTrace();
  59.                         }
  60.                
  61.                 numeroThreads = 0;
  62.                 lock.unlock();
  63.                
  64.         }
  65.        
  66.         public void doneP(boolean nera){
  67.        
  68.                 lock.lock();
  69.                 ++numeroThreads;
  70.                 if(nera)
  71.                         this.nera = true;
  72.                
  73.                 cond.signal();
  74.                
  75.                 lock.unlock();
  76.         }
  77.        
  78. }
  79.  
  80. class Pecora{
  81.        
  82.         private boolean bianca;
  83.  
  84.         public Pecora(){
  85.                 bianca = true;
  86.        
  87.         }
  88.        
  89.         public void vernicia(){
  90.                 bianca = false;
  91.         }
  92.        
  93.         public boolean isBianca(){
  94.                 return bianca;
  95.         }
  96.  
  97.  
  98.  
  99.         public void tosa() {
  100.                 bianca = true;
  101.                
  102.         }
  103.        
  104. }
  105.  
  106. class Gregge{
  107.        
  108.         private static final int numeroThreads = 4;
  109.         private Pecora[] gregge;
  110.         private ReentrantLock lock;
  111.         private Condition cond;
  112.         private int pastori, tosatori,lupi;
  113.        
  114.         public Gregge(int dimensione){
  115.                 this.gregge = new Pecora[dimensione];
  116.                 this.lock = new ReentrantLock();
  117.                 this.cond = lock.newCondition();
  118.                 this.pastori = 0;
  119.                 this.tosatori = 0;
  120.                 this.lupi = 0;
  121.         }
  122.        
  123.        
  124.         public void takePastoreLock(){
  125.                 lock.lock();
  126.                 while(lupi > 0 || tosatori > 0)
  127.                         try {
  128.                                 cond.await();
  129.                         } catch (InterruptedException e) {
  130.                                 // TODO Auto-generated catch block
  131.                                 e.printStackTrace();
  132.                         }
  133.                 ++pastori;
  134.                 lock.unlock();
  135.         }
  136.        
  137.         public void takeLupoLock(){
  138.                 lock.lock();
  139.                 while(pastori > 0 || tosatori > 0)
  140.                         try {
  141.                                 cond.await();
  142.                         } catch (InterruptedException e) {
  143.                                 // TODO Auto-generated catch block
  144.                                 e.printStackTrace();
  145.                         }
  146.                 ++lupi;
  147.                 lock.unlock();
  148.         }
  149.        
  150.         public void takeTosatoreLock(){
  151.                 lock.lock();
  152.                 while(lupi > 0 || pastori > 0)
  153.                         try {
  154.                                 cond.await();
  155.                         } catch (InterruptedException e) {
  156.                                 // TODO Auto-generated catch block
  157.                                 e.printStackTrace();
  158.                         }
  159.                 ++tosatori;
  160.                 lock.unlock();
  161.         }
  162.        
  163.         public void leaveLupoLock(){
  164.                
  165.                 lock.lock();
  166.                 --lupi;
  167.                 if(lupi == 0)
  168.                         cond.signalAll();
  169.                 lock.unlock();
  170.         }
  171.        
  172.         public void leaveTosatoreLock(){
  173.                
  174.                 lock.lock();
  175.                
  176.                 tosatori = 0;
  177.                         cond.signalAll();
  178.                
  179.                 lock.unlock();
  180.                
  181.         }
  182.        
  183.         public void leavePastoreLock(){
  184.                 lock.lock();
  185.                 pastori =  0;
  186.                         cond.signalAll();
  187.                
  188.                 lock.unlock();
  189.                
  190.         }
  191.         public void startGregge(){
  192.                
  193.                 for(int i = 0; i<gregge.length; i++)
  194.                         gregge[i] = new Pecora();
  195.                
  196.                 Lupo lupo1 = new Lupo(this);
  197.                 Lupo lupo2 = new Lupo(this);
  198.                
  199.                 lupo1.start();
  200.                 lupo2.start();
  201.                
  202.                 Display display = new Display(this);
  203.                 display.start();
  204.                
  205.         }
  206.        
  207.        
  208.        
  209.         public boolean esistePecoraNera(){
  210.                
  211.                 int k = numeroThreads;
  212.                 int fetta;
  213.                 SpecialBarriera barriera = new SpecialBarriera(numeroThreads);
  214.        
  215.                
  216.                 while((fetta = gregge.length/numeroThreads) == 0)
  217.                         --k;
  218.                
  219.                 Pastore[] pastori = new Pastore[k];
  220.                
  221.                 for(int i = 0; i<k-1;i++){
  222.                         takePastoreLock();
  223.                         pastori[i] = new Pastore(fetta*i,fetta*(i+1)-1,gregge,barriera);
  224.                         pastori[i].start();
  225.                 }
  226.                 takePastoreLock();
  227.                 pastori[k-1] = new Pastore(fetta*(k-1),gregge.length-1,gregge,barriera);
  228.                 pastori[k-1].start();
  229.                
  230.                 boolean nera = barriera.awaitP();
  231.  
  232.                 //BUG perchè i thread non finiscono in realtà....
  233.                 leavePastoreLock();
  234.        
  235.                 return nera;
  236.                
  237.                
  238.         }
  239.        
  240.        
  241.         public void tosaTutte(){
  242.                
  243.                 int x = numeroThreads;
  244.                 int fetta;
  245.                 SpecialBarriera barriera = new SpecialBarriera(numeroThreads);
  246.                
  247.                 while((fetta = gregge.length/numeroThreads) == 0)
  248.                         --x;
  249.                
  250.                 Tosatore[] tosatori =  new Tosatore[x];
  251.                
  252.                 for(int i = 0; i<x-1;i++){
  253.                         takeTosatoreLock();
  254.                         tosatori[i] = new Tosatore(fetta*i,fetta*(i+1)-1,gregge,barriera);
  255.                         tosatori[i].start();
  256.                 }
  257.                 takeTosatoreLock();
  258.                 tosatori[x-1] = new Tosatore(fetta*(x-1),gregge.length-1,gregge,barriera);
  259.                 tosatori[x-1].start();
  260.        
  261.                 barriera.awaitT();
  262.                 for(int i= 0; i<gregge.length; i++)
  263.                         if(gregge[i].isBianca())
  264.                                 System.out.print("O");
  265.                         else System.out.print("@");
  266.                 System.out.println();
  267.                 leaveTosatoreLock();
  268.         }
  269.  
  270.         public void alteraColore() {
  271.                
  272.                 takeLupoLock();
  273.  
  274.                 // attualmente becca anche le nere XD
  275.                 int rand = (int) (Math.random()*gregge.length);
  276.                
  277.                 gregge[rand].vernicia();
  278.                
  279.                 leaveLupoLock();
  280.         }
  281.        
  282.  
  283.  
  284.         public void stampa() {
  285.                
  286.                 lock.lock();
  287.                
  288.                        
  289.                         int i = 0;
  290.                         int k = gregge.length/5;
  291.                
  292.                         for(int j = 1; j<=k; j++){
  293.                                 for(; i<j*5;i++)
  294.                                         if(gregge[i].isBianca())
  295.                                                 System.out.print("O");
  296.                                         else System.out.print("@");
  297.                         System.out.println("");
  298.                         }
  299.                        
  300.                         for(; i<gregge.length;i++)
  301.                                 if(gregge[i].isBianca())
  302.                                         System.out.print("O");
  303.                                 else System.out.print("@");
  304.                         System.out.println("]");
  305.                
  306.                 lock.unlock();
  307.                
  308.         }
  309.        
  310.        
  311. }
  312.  
  313. class Display extends Thread{
  314.        
  315.         private Gregge gregge;
  316.        
  317.         public Display(Gregge gregge){
  318.                 this.gregge = gregge;
  319.         }
  320.  
  321.         @Override
  322.         public void run() {
  323.                 while(true){
  324.                         try {
  325.                                 sleep(2000);
  326.                         } catch (InterruptedException e) {
  327.                                 // TODO Auto-generated catch block
  328.                                 e.printStackTrace();
  329.                         }
  330.                         gregge.stampa();
  331.                        
  332.                 }
  333.         }
  334. }
  335.  
  336. class Lupo extends Thread{
  337.         Gregge gregge;
  338.         public Lupo(Gregge gregge){
  339.                 this.gregge = gregge;
  340.         }
  341.        
  342.        
  343.         @Override
  344.         public void run() {
  345.                 while(true){
  346.                         try {
  347.                                 sleep((int)(1000));
  348.                         } catch (InterruptedException e) {
  349.                                 // TODO Auto-generated catch block
  350.                                 e.printStackTrace();
  351.                         }
  352.                         gregge.alteraColore();
  353.                 }
  354.         }
  355. }
  356.  
  357. class Pastore extends Thread{
  358.        
  359.         private int min;
  360.         private int max;
  361.         private  Pecora[] gregge;
  362.         private SpecialBarriera barriera;
  363.        
  364.         public Pastore(int min, int max, Pecora[] gregge, SpecialBarriera barriera){
  365.                 this.min = min;
  366.                 this.max = max;
  367.                 this.gregge = gregge;
  368.                 this.barriera = barriera;
  369.                
  370.         }
  371.        
  372.         @Override
  373.         public void run() {
  374.                
  375.         boolean nera = false;
  376.         for(; min <= max; min++)
  377.                 if(!gregge[min].isBianca())
  378.                         nera = true;
  379.                
  380.                 barriera.doneP(nera);
  381.         }
  382. }
  383.  
  384. class Tosatore extends Thread{
  385.         private int min;
  386.         private int max;
  387.         private  Pecora[] gregge;
  388.         private SpecialBarriera barriera;
  389.        
  390.         public Tosatore(int min, int max, Pecora[] gregge, SpecialBarriera barriera){
  391.                 this.min = min;
  392.                 this.max = max;
  393.                 this.gregge = gregge;
  394.                 this.barriera = barriera;
  395.         }
  396.        
  397.         @Override
  398.         public void run() {
  399.                 System.out.println(min+" "+max);
  400.                 for(; min <= max; min++)
  401.                         if(!gregge[min].isBianca()){
  402.                                 gregge[min].tosa();
  403.                         }
  404.                         barriera.doneT();
  405.                
  406.         }
  407. }
  408.  
  409.  
  410.  
  411.  
  412.  
  413. public class GreggeMain {
  414.         public static void main(String[] args) {
  415.                
  416.                 Gregge gregge = new Gregge(11);
  417.                 gregge.startGregge();
  418.         while(true){   
  419.                 try {
  420.                         Thread.sleep(5000);
  421.                 } catch (InterruptedException e) {
  422.                         // TODO Auto-generated catch block
  423.                         e.printStackTrace();
  424.                 }
  425.                
  426.                 if(gregge.esistePecoraNera())
  427.                         gregge.tosaTutte();
  428.         }
  429.         }
  430. }