Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class FirBlocabil extends Thread{ // clasa parinte pentru toate firele de executie
- // testate de acest program
- private Sondare sondaj;
- protected JTextField stare=new JTextField(30);//afiseaza inf. despre obiect
- protected int i;
- public FirBlocabil(Container c){
- c.add(stare);
- sondaj = new Sondare(this,c);
- }
- public synchronized int citire(){return i;}
- protected synchronized void actualizare()
- {
- stare.setText(getClass().getName()+"are starea: "+i);
- }
- public void opresteSondare()
- {
- sondaj.terminare();
- }
- }
- class Sondare extends Thread{ //clasacestarteazafiecareobiectFirBlocabil
- private FirBlocabil fb;
- private int sesiune;
- private JTextField stareSondare=new JTextField(30);
- private boolean oprire=false;
- public Sondare(FirBlocabil fb, Container c){
- c.add(stareSondare);
- this.fb=fb;
- start();}
- public void terminare(){oprire=true;}
- public void run(){
- while(!oprire){
- stareSondare.setText(fb.getClass().getName()+"Sondare"+
- (++sesiune)+";valoare= "+fb.citire());
- try{sleep(100);}catch(InterruptedException e){}}}}
- class ApelSleep1 extends FirBlocabil{// blocare prin apel sleep()
- public ApelSleep1(Container c){super(c);}
- public synchronized void run(){
- while(true){
- i++;
- actualizare();
- try{sleep(1000);}catch(InterruptedException e){}}}}
- class ApelSleep2 extends FirBlocabil{// blocare prin apel sleep()
- public ApelSleep2(Container c){super(c);}
- public void run(){
- while(true){
- schimba();
- try{sleep(1000);}catch(InterruptedException e){}}}
- public synchronized void schimba(){i++;actualizare();}}
- class ApelSuspend1 extends FirBlocabil{
- public ApelSuspend1(Container c){
- super(c);
- new ApelResume(this);
- }
- }
- class ApelSuspend2 extends ApelSuspend1{
- public ApelSuspend2(Container c){super(c);}
- public synchronized void run(){
- while(true){
- i++;
- actualizare();
- suspend();}}} //nu este preferat acest apel in Java 1.2
- // deoarece pe perioada blocarii nu se pot accesa resursele blocate de acest
- // firse prefera apelul Object.wait();
- class ApelSuspend3 extends ApelSuspend1{
- public ApelSuspend3(Container c){super(c);}
- public void run(){
- while(true){
- schimba();
- suspend();}}
- public synchronized void schimba(){i++;actualizare();}}
- class ApelResume extends Thread{
- private ApelSuspend1 as;
- public ApelResume(ApelSuspend1 as){this.as=as;start();}
- public void run(){
- while(true){
- try{sleep(1000);}catch(InterruptedException e){}
- as.resume(); }}}// poateproduce deadlock in Java 1.2 se prefera notify();
- class ApelWait1 extends FirBlocabil{
- public ApelWait1(Container c){super(c);}
- public synchronized void run(){
- while(true){i++; actualizare();
- try{wait(1000);}catch(InterruptedException e){}}}}
- class ApelWait2 extends FirBlocabil{
- public ApelWait2(Container c){super(c);new ApelNotify(this);}
- public synchronized void run(){
- while(true){i++;actualizare();try{wait();}catch(InterruptedException e){}}}}
- class ApelNotify extends Thread{
- private ApelWait2 aw2;
- public ApelNotify(ApelWait2 aw2)
- {
- this.aw2=aw2;start();
- }
- public void run(){
- while(true)
- {
- try{sleep(2000);}
- catch(InterruptedException e){}
- synchronized(aw2){aw2.notify();}}}}
- class Expeditor extends FirBlocabil{
- private Writer out;
- public Expeditor(Container c, Writer out){super(c);this.out=out;}
- public void run(){
- while(true){
- for(char c='A';c<='z';c++)
- try{i++;out.write(c);stare.setText("expeditorul a trimis:"+(char)c);
- sleep((int)(3000*Math.random()));
- }catch(InterruptedException e){}
- catch(IOException e){}}}}
- class Destinatar extends FirBlocabil{
- private Reader in;
- public Destinatar(Container c, Reader in){super(c);this.in=in;}
- public void run(){
- try{while(true){i++;stare.setText("Destinatarul a citit: "+(char)in.read());}
- }catch(IOException e){e.printStackTrace();}}}
- public class BlocareFirExecutie extends JApplet {
- private JButton
- pornire=new JButton("Pornire"),
- oprireSondare=new JButton("Oprire sondare");
- private boolean activat=false; private FirBlocabil[] fire;
- private PipedWriter out;
- private PipedReader in;
- public void init(){
- Container cp=getContentPane();
- cp.setLayout(new FlowLayout());
- out=new PipedWriter();
- try{
- in=new PipedReader(out);
- }catch(IOException e){}
- fire=new FirBlocabil[]{new ApelSleep1(cp), new ApelSleep2(cp),
- new ApelSuspend2(cp), new ApelSuspend3(cp),
- new ApelWait1(cp),new ApelWait2(cp),
- new Expeditor(cp,out),new Destinatar(cp,in)};
- pornire.addActionListener(new ActiunePornire());cp.add(pornire);
- oprireSondare.addActionListener(new ActiuneOprireSondare());
- cp.add(oprireSondare);}
- class ActiunePornire implements ActionListener {
- public void actionPerformed(ActionEvent e){
- if(!activat){
- activat=true;
- for(int i=0;i<fire.length;i++) fire[i].start();}}}
- class ActiuneOprireSondare implements ActionListener{
- public void actionPerformed(ActionEvent e){
- for(int i=0;i<fire.length;i++) fire[i].opresteSondare();}}
- public static void main(String[] args){
- JApplet aplet=new BlocareFirExecutie();
- JFrame cadru=new JFrame("Blocare Fir Executie");
- cadru.addWindowListener(
- new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);}});
- cadru.getContentPane().add(aplet);
- cadru.setSize(350,550);
- aplet.init();
- aplet.start();
- cadru.setVisible(true); }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement