Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class BoundedBuffer {
- public BoundedBuffer() {
- // buffer diinisialisasikan kosong
- count = 0;
- in = 0;
- out = 0;
- buffer = new Object[BUFFER_SIZE];
- }
- // produser memanggil method ini
- public void enter( Object item ) {
- while ( count == BUFFER_SIZE )
- ; // do nothing
- // menambahkan suatu item ke dalam buffer
- ++count;
- buffer[in] = item;
- in = ( in + 1 ) % BUFFER_SIZE;
- if ( count == BUFFER_SIZE )
- System.out.println( "Producer Entered " +
- item + " Buffer FULL" );
- else
- System.out.println( "Producer Entered " +
- item + " Buffer Size = " + count );
- }
- // consumer memanggil method ini
- public Object remove() {
- Object item ;
- while ( count == 0 )
- ; // do nothing
- // menyingkirkan suatu item dari buffer
- --count;
- item = buffer[out];
- out = ( out + 1 ) % BUFFER_SIZE;
- if ( count == 0 )
- System.out.println( "Consumer consumed " +
- item + " Buffer EMPTY" );
- else
- System.out.println( "Consumer consumed " +
- item + " Buffer Size = " +count );
- return item;
- }
- public static final int NAP_TIME = 5;
- private static final int BUFFER_SIZE = 5;
- private volatile int count;
- private int in; // arahkan ke posisi kosong selanjutnya
- private int out; // arahkan ke posisi penuh selanjutnya
- private Object[] buffer;
- }
Advertisement
Add Comment
Please, Sign In to add comment