CS 3530 Quiz 4
Kennesaw
State University
Department
of Computer Science
Operating
Systems CS3530/01
Quiz
# 4 / Synchronization & Threads
Dalibor
Labudovic
06/30/12013
Initial
Problem Statement:
- Carry out a research on threads using the Java programming language.
- Develop a simplified version of the producer-consumer synchronization problem in Java.
- Test your results and include a report (similar to the assignments).
Summary and
purpose of the assignment activity:
The
purpose of this assignment is to research the topic of threads and
producer-consumer synchronization in order to develop a working Java
sample program. From better understanding of the thread theory, as
programmers we are able to utilize better programing ethics. With
better programming understanding and ethics, our programs will be
more reliable and stable.
Detail
description of the solution and used in the project:
Description: In this
Java program, there are 4 classes including the main class. There is
a Buffer class which will enclose two important methods. One of those
methods is the ''put'' synchronization method, which places a
producer process in the buffer. The other method is the ''get''
synchronization method, which gets a consumer process from the
buffer. In both method cases, the synchronization policy is to check
for availability on empty slot or to check if there is a process to
grab from the buffer.
public
class
ProducerConsumerTest
{
public
static
void
main(String[] args) {
Buffer c = new
Buffer();//object
for the buffer
Producer p1 = new
Producer(c, 1);//object
for the producer
Consumer c1 = new
Consumer(c, 1);//object
for the consumer
p1.start();
c1.start();
}
}
//buffer
class
class
Buffer {
private
int
contents;
private
boolean
available
= false;
public
synchronized
int
get() {
while
(available
== false)
{
try
{
wait();
}
catch
(InterruptedException e) {
}
}
available
= false;
notifyAll();
return
contents;
}
public
synchronized
void
put(int
value) {
while
(available
== true)
{
try
{
wait();
}
catch
(InterruptedException e) {
}
}
contents
= value;
available
= true;
notifyAll();
}
}
//consumer
class
class
Consumer extends
Thread {
private
Buffer buffer;
private
int
number;
public
Consumer(Buffer c, int
number) {
buffer
= c;
this.number
= number;
}
public
void
run() {
int
value = 0;
for
(int
i = 0; i < 10; i++) {
value = buffer.get();
System.out.println("Consumer
#"
+
this.number
+
"
got: "
+ value);
}
}
}
//producer
class
class
Producer extends
Thread {
private
Buffer buffer;
private
int
number;
public
Producer(Buffer c, int
number) {
buffer
= c;
this.number
= number;
}
public
void
run() {
for
(int
i = 0; i < 10; i++) {
buffer.put(i);
System.out.println("Producer
#"
+ this.number
+
"
put: "
+ i);
try
{
sleep((int)(Math.random()
* 100));
}
catch
(InterruptedException e) { }
}
}
}
Table
of results:
The results are the
printout of each Producer and Consumer results, with the simplicity
of this simulation, there were only one producer and consumer
processing ten processes in total.
Producer
#1 put: 0
Consumer
#1 got: 0
Producer
#1 put: 1
Consumer
#1 got: 1
Producer
#1 put: 2
Consumer
#1 got: 2
Producer
#1 put: 3
Consumer
#1 got: 3
Producer
#1 put: 4
Consumer
#1 got: 4
Producer
#1 put: 5
Consumer
#1 got: 5
Producer
#1 put: 6
Consumer
#1 got: 6
Producer
#1 put: 7
Consumer
#1 got: 7
Producer
#1 put: 8
Consumer
#1 got: 8
Producer
#1 put: 9
Consumer
#1 got: 9
Comments
and Conclusion:
In the conclusion, this assignment has caught me how to utilize the thread library
class within Java. Additionally, the use of extension of thread library class, I also
learned the usage of the synchronization policies used to organize processes produced
and processes consumed. Along with the importance of usage of the buffer class which
stored the processes I also learned to utilize a simple binary semaphore which checked
availability in the buffer.
Comments
Post a Comment