Advertisement
Adytzu04

BlocareFirExecutie c10

Dec 10th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6.  
  7. class FirBlocabil extends Thread{ // clasa parinte pentru toate firele de executie
  8.     // testate de acest program
  9.     private Sondare sondaj;
  10.     protected JTextField stare=new JTextField(30);//afiseaza inf. despre obiect
  11.     protected int i;
  12.     public FirBlocabil(Container c){
  13.         c.add(stare);
  14.         sondaj = new Sondare(this,c);
  15.         }
  16.     public synchronized int citire(){return i;}
  17.     protected synchronized void actualizare()
  18.     {
  19.         stare.setText(getClass().getName()+"are starea: "+i);
  20.     }
  21.     public void opresteSondare()
  22.     {
  23.         sondaj.terminare();
  24.     }
  25.     }
  26.  
  27. class Sondare extends Thread{ //clasacestarteazafiecareobiectFirBlocabil
  28.     private FirBlocabil fb;
  29.     private int sesiune;
  30.     private JTextField stareSondare=new JTextField(30);
  31.     private boolean oprire=false;
  32.     public Sondare(FirBlocabil fb, Container c){
  33.         c.add(stareSondare);
  34.         this.fb=fb;
  35.         start();}
  36.     public void terminare(){oprire=true;}
  37.     public void run(){
  38.         while(!oprire){
  39.             stareSondare.setText(fb.getClass().getName()+"Sondare"+
  40.                     (++sesiune)+";valoare= "+fb.citire());
  41.             try{sleep(100);}catch(InterruptedException e){}}}}
  42.  
  43. class ApelSleep1 extends FirBlocabil{// blocare prin apel sleep()
  44.     public ApelSleep1(Container c){super(c);}
  45.     public synchronized void run(){
  46.         while(true){
  47.             i++;
  48.             actualizare();
  49.             try{sleep(1000);}catch(InterruptedException e){}}}}
  50.  
  51. class ApelSleep2 extends FirBlocabil{// blocare prin apel sleep()
  52.     public ApelSleep2(Container c){super(c);}
  53.     public void run(){
  54.         while(true){
  55.             schimba();
  56.             try{sleep(1000);}catch(InterruptedException e){}}}
  57.     public synchronized void schimba(){i++;actualizare();}}
  58.  
  59. class ApelSuspend1 extends FirBlocabil{
  60.     public ApelSuspend1(Container c){
  61.         super(c);
  62.         new ApelResume(this);
  63.         }
  64.     }
  65. class ApelSuspend2 extends ApelSuspend1{
  66.     public ApelSuspend2(Container c){super(c);}
  67.     public synchronized void run(){
  68.         while(true){
  69.             i++;
  70.             actualizare();
  71.             suspend();}}} //nu este preferat acest apel in Java 1.2
  72. // deoarece pe perioada blocarii nu se pot accesa resursele blocate de acest
  73. // firse prefera apelul Object.wait();
  74.  
  75. class ApelSuspend3 extends ApelSuspend1{
  76.     public ApelSuspend3(Container c){super(c);}
  77.     public void run(){
  78.         while(true){
  79.             schimba();
  80.             suspend();}}
  81.     public synchronized void schimba(){i++;actualizare();}}
  82. class ApelResume extends Thread{
  83.     private ApelSuspend1 as;
  84.     public ApelResume(ApelSuspend1 as){this.as=as;start();}
  85.     public void run(){
  86.         while(true){
  87.             try{sleep(1000);}catch(InterruptedException e){}
  88.         as.resume(); }}}// poateproduce deadlock in Java 1.2 se prefera notify();
  89.  
  90. class ApelWait1 extends FirBlocabil{
  91.     public ApelWait1(Container c){super(c);}
  92.     public synchronized void run(){
  93.         while(true){i++; actualizare();
  94.         try{wait(1000);}catch(InterruptedException e){}}}}
  95. class ApelWait2 extends FirBlocabil{
  96.     public ApelWait2(Container c){super(c);new ApelNotify(this);}
  97.     public synchronized void run(){
  98.         while(true){i++;actualizare();try{wait();}catch(InterruptedException e){}}}}
  99. class ApelNotify extends Thread{
  100.     private ApelWait2 aw2;
  101.     public ApelNotify(ApelWait2 aw2)
  102.     {
  103.         this.aw2=aw2;start();
  104.     }
  105.     public void run(){
  106.         while(true)
  107.         {
  108.             try{sleep(2000);}
  109.             catch(InterruptedException e){}
  110.         synchronized(aw2){aw2.notify();}}}}
  111.  
  112. class Expeditor extends FirBlocabil{
  113.     private Writer out;
  114.     public Expeditor(Container c, Writer out){super(c);this.out=out;}
  115.     public void run(){
  116.         while(true){
  117.             for(char c='A';c<='z';c++)
  118.                 try{i++;out.write(c);stare.setText("expeditorul a trimis:"+(char)c);
  119.                 sleep((int)(3000*Math.random()));
  120.                 }catch(InterruptedException e){}
  121.             catch(IOException e){}}}}
  122. class Destinatar extends FirBlocabil{
  123.     private Reader in;
  124.     public Destinatar(Container c, Reader in){super(c);this.in=in;}
  125.     public void run(){
  126.         try{while(true){i++;stare.setText("Destinatarul a citit: "+(char)in.read());}
  127.         }catch(IOException e){e.printStackTrace();}}}
  128. public class BlocareFirExecutie extends JApplet {
  129.     private JButton
  130.     pornire=new JButton("Pornire"),
  131.     oprireSondare=new JButton("Oprire sondare");
  132.     private boolean activat=false; private FirBlocabil[] fire;
  133.     private PipedWriter out;
  134.     private PipedReader in;
  135.     public void init(){
  136.         Container cp=getContentPane();
  137.         cp.setLayout(new FlowLayout());
  138.         out=new PipedWriter();
  139.         try{
  140.             in=new PipedReader(out);
  141.         }catch(IOException e){}
  142.         fire=new FirBlocabil[]{new ApelSleep1(cp), new ApelSleep2(cp),
  143.                 new ApelSuspend2(cp), new ApelSuspend3(cp),
  144.                 new ApelWait1(cp),new ApelWait2(cp),
  145.                 new Expeditor(cp,out),new Destinatar(cp,in)};
  146.         pornire.addActionListener(new ActiunePornire());cp.add(pornire);
  147.         oprireSondare.addActionListener(new ActiuneOprireSondare());
  148.         cp.add(oprireSondare);}
  149.     class ActiunePornire implements ActionListener {
  150.         public void actionPerformed(ActionEvent e){
  151.             if(!activat){
  152.                 activat=true;
  153.                 for(int i=0;i<fire.length;i++) fire[i].start();}}}
  154.     class ActiuneOprireSondare implements ActionListener{
  155.         public void actionPerformed(ActionEvent e){
  156.             for(int i=0;i<fire.length;i++) fire[i].opresteSondare();}}
  157.     public static void main(String[] args){
  158.         JApplet aplet=new BlocareFirExecutie();
  159.         JFrame cadru=new JFrame("Blocare Fir Executie");
  160.         cadru.addWindowListener(
  161.                 new WindowAdapter(){
  162.                     public void windowClosing(WindowEvent e){
  163.                         System.exit(0);}});
  164.         cadru.getContentPane().add(aplet);
  165.         cadru.setSize(350,550);
  166.         aplet.init();
  167.         aplet.start();
  168.         cadru.setVisible(true); }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement