Loading

İletişim

muhammedozturk@sakarya.edu.tr

+90 (264) 295 69 09

8. hafta işletim

package deneme;

import java.util.Map;

import java.lang.*;

 

public class TestThread {

               public final static int NUMTHREADS   = 3;

               public static int sharedData  = 0;

               public static int sharedData2 = 0;

               static class theLock extends Object {

               }

               static public theLock lockObject1 = new theLock();

             

              // static public theLock lockObject = new theLock();

             

               static class theThread extends Thread {

                  public void run() {

                     System.out.print("Thread " + getName() + ": Girdi\n");

                     synchronized (lockObject1) {

                        /********** Kritik Bölge *******************/

                        System.out.print("Thread " + getName() +

                                         ": Kritik bölge başla, senkron blok tutuldu\n");

                        ++sharedData; --sharedData2;

                        System.out.print("Thread " + getName() +

                                         ": Kritik bölge bitir, senkron bloğu bırak\n");

                        /********** Kritik bölge *******************/

                     }

                  }

               }

              /**Main*/

              public static void main(String[] args) throws InterruptedException

              {

                          theThread threads[] = new theThread[NUMTHREADS];

                  System.out.print("Test işlemine gir\n");

             

                  System.out.print("Paylasimli veriye erisimi engellemek icin senkronize et\n");

                  synchronized (lockObject1) {

             

                     System.out.print("Thread olustur\n");

                     for (int i=0; i<NUMTHREADS; ++i) {

                     threads[i] = new theThread();

                        threads[i].start();

                     }

             

                     System.out.print("Paylasilan veri isi icin bekle\n");

                     try {

                        Thread.sleep(3000);

                     }

                     catch (InterruptedException e) {

                        System.out.print("bekleme bitti\n");

                     }

                     System.out.print("kilitli veriyi ac\n");

                  }

             

                  System.out.print("Threadleri isinin bitmesi icin bekle\n");

                  for(int i=0; i <NUMTHREADS; ++i) {

                  threads[i].join();

                     }

               

                  }

             

           

                       

              }