Skip navigation


Setelah mengimplementasikan Multithread di postingan sebelumnya, jadi pengen iseng-iseng nyoba salah satu problem di IPC yakni Readers and Writers. Sebelumnya di kuliah High Performance System, pernah dikasih tugas yang menyangkut IPC problem juga yakni Producer dan Consumer, tapi pakai bahasa C dan pakai  POSIX thread. Sekarang ingin coba implementasi di Java.

Permasalahan Readers and Writers ini tidak jauh berbeda dengan Dining Philosopher, hanya saja di Readers and Writers ini terdapat beberapa Readers dan Writers. Kedua jenis ini memiliki perlakuan yang berbeda. Sebuah filefile boleh diakses beberapa Reader bersamaan, tetapi ketika Writer sedang menulis tidak ada yang boleh menulis file tersebut.

Masalah ini bisa dilihat setelah kita memodifikasi method untuk write dan read pada kode di postingan sebelumnya.

public void read() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": start reading!");
            try {
                FileInputStream fis = new FileInputStream(fileName);
                byte[] b = new byte[5];
                fis.read(b);
                System.out.println(strid + ": read \"" + new String(b) + "\"");
                fis.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            System.out.println(strid + ": done reading!");
        }
    }
public void write() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": start writing!");
            try {
                FileOutputStream fos = new FileOutputStream(fileName);
                String srandom = Long.toString((long) ((new Random()).nextDouble() * 9000) + 1000);
                //Writing random
                System.out.println(strid + ": write \"" + srandom + "\"");
                fos.write(srandom.getBytes());
                fos.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            System.out.println(strid + ": done writing!");
        }
    }

Jika kode ini dijalankan maka keluarannya dapat berupa

Reader 1: start!
Writer 1: start!
Reader 2: start!
Writer 1: start writing!
Reader 1: start reading!
Reader 2: start reading!
Reader 2: read "     "
Reader 2: done reading!
Reader 1: read "     "
Reader 2: start reading!
Reader 1: done reading!
Reader 1: start reading!
Reader 2: read "     "
Writer 1: write "8727"
Reader 2: done reading!
Reader 1: read "     "
Reader 2: start reading!
Reader 1: done reading!
Reader 1: start reading!
Writer 1: done writing!
Writer 1: start writing!
Reader 2: read "8727 "
Reader 1: read "8727 "
Reader 2: done reading!
Reader 2: start reading!
Reader 2: read "     "
Reader 1: done reading!
Reader 2: done reading!
Reader 1: start reading!
Reader 2: start reading!
Writer 1: write "9972"
Reader 1: read "9972 "
Writer 1: done writing!
Reader 2: read "9972 "
Reader 1: done reading!
Reader 2: done reading!
Writer 1: start writing!
Reader 2: start reading!
Reader 1: start reading!
Reader 2: read "     "
Writer 1: write "7951"
Reader 2: done reading!
Reader 1: read "     "
Reader 2: start reading!
Writer 1: done writing!
Writer 1: start writing!
Reader 1: done reading!
Reader 1: start reading!
Reader 2: read "7951 "
...

Dapat dilihat hasilnya agak kacau karena tidak ada sinkronisasi sama sekali.🙂 Kadang-kadang si Readernya tidak mendapat hasil apa-apa karena berkas sedang di tulis.
Kodenya dapat diunduh di sini. (rename extension ke zip dulu)

One Comment

  1. bang, terimaksih atas codingnya, oiiya bang mau tanyak ni bang, etelah saya coba, ada satu yg error ni bang, di method write “FileOutputStream fos = new FileOutputStream(fileName);”. bisa minta bantuannyaa bang?
    terimaksih sebelumnya..


One Trackback/Pingback

  1. […] Males Koding Blog yang isinya ternyata banyak kodingannya AboutRequestIn Draft « The Readers and Writers Problem – Part 1 […]

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: