Skip navigation


Cuman snippets aja, kadang-kadang suka lupa gimana cara pakai multithread di Java. ^_^

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Petra Barus
 */
public class ReadWrite1 implements Runnable {

    //
    public static int countwriter = 0;
    public static int countreader = 0;
    //
    public static final int READER = 0;
    public static final int WRITER = 1;
    //
    private int type;
    private int id;
    private String strid;

    public ReadWrite1(int type) {
        this.type = type;
        this.strid = (type == READER) ? ("Reader " + (this.id = ++countreader)) : ("Writer " + (this.id = ++countwriter));
    }

    public static void main(String[] argv) {
        (new Thread(new ReadWrite1(READER))).start();
        (new Thread(new ReadWrite1(READER))).start();
        (new Thread(new ReadWrite1(WRITER))).start();
    }

    public void run() {
        if (type == READER) {
            read();
        } else if (type == WRITER) {
            write();
        }
    }

    //*********************************************************
    // Reader
    //*********************************************************
    /**
     *
     */
    public void read() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": reading!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(ReadWrite1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    //*********************************************************
    //*********************************************************
    // Writer
    //*********************************************************

    public void write() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": writing!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(ReadWrite1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    //*********************************************************
}

Contoh keluarannya

Reader 1: start!
Reader 2: start!
Reader 2: reading!
Reader 1: reading!
Writer 1: start!
Writer 1: writing!
Reader 1: reading!
Reader 2: reading!
Writer 1: writing!
Reader 1: reading!
Reader 2: reading!
Writer 1: writing!
Reader 2: reading!
Reader 1: reading!
Writer 1: writing!

One Trackback/Pingback

  1. […] Males Koding Blog yang isinya ternyata banyak kodingannya AboutRequestIn Draft « Contoh Multithread di Java […]

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: