i have to go into thread synchronizing and have therefore
sorry, didnt want to post so soon =)
so, i have to go into thread synchronizing and therefore i have written the school example with consumer/producer, which use a ringbuffer. to control whats happening i have put some sysos in the synchronized block of lies()/schreib() to get
the actual operation and after that the current size of the buffer. but i get some strange result, sometimes read and write operations are displayed in one cyclus, sometimes the syso doesnt even appear.
please take a look at the code
main program
**************
class ThreadTest2
{
public static void main(String args[])
{
System.out.println("ThreadTest2 started ...");
Ringpuffer buffer = new Ringpuffer(30);
Producer producer = new Producer(buffer, false);
Consumer consumer = new Consumer(buffer, false);
System.out.println("Producer und Consumer werden gestartet\n");
producer.start();
consumer.start();
try
{
producer.join();
consumer.join();
}
catch(InterruptedException e)
{
System.err.println("Fehler bei Thread.join\n"+e);
}
System.out.println("\n\n*** fertig ***");
}
}
ringbuffer
**************
class Ringpuffer
{
private Integer[] buffer;
private int in, out, currSize, maxSize;
public Ringpuffer(int size)
{
buffer = new Integer[size];
maxSize = size;
}
public synchronized void schreib(int value)
{
// if(currSize<maxSize)
{
while(currSize == maxSize)
try
{
System.out.print("\n__Puffer voll");
wait();
}
catch(InterruptedException e)
{
System.err.println("Fehler bei Thread.wait\n"+e);
}
currSize++;
buffer[in++] = new Integer(value);
if(in==maxSize) in=0;
System.out.print("\t geschrieben, size: "+currSize);
notify();
}
// else System.out.print("\t --> Wert "+value+" nicht geschrieben:Puffer voll !\n");
}
public synchronized Integer lies()
{
// if(currSize>0)
{
while(currSize == 0)
try
{
System.out.print("\n__Puffer leer");
wait();
}
catch(InterruptedException e)
{
System.err.println("Fehler bei Thread.wait\n"+e);
}
int aktpos=out;
if((++out)==maxSize) out = 0;
currSize--;
System.out.print("\t gelesen, size: "+currSize);
notify();
return(buffer[aktpos]);
}
}
public int getCurrSize()
{
return currSize;
}
}
producer
********
import java.util.Random;
class Producer extends Thread
{
final int max_thread_sleep = 300 + 1;
final int max_generated_number = 500 + 1;
boolean wait = true;
Ringpuffer buffer;
Random rand = new Random();
public Producer(Ringpuffer buffer, boolean wait)
{
this.buffer = buffer;
this.wait = wait;
}
public void run()
{
for(int i=0; i<=50; i++)
{
System.out.print("\n+Producer: "+i);
buffer.schreib(i);
// buffer.schreib(rand.nextInt(max_generated_number));
if(wait)
try
{
this.sleep(rand.nextInt(max_thread_sleep));
}
catch(InterruptedException e)
{
System.err.print("\nProducer: Fehler bei Thread.sleep\n"+e);
}
}
}
}
consumer
********
import java.util.Random;
class Consumer extends Thread
{
final int max_thread_sleep = 300 + 1;
boolean wait = true;
Ringpuffer buffer;
Random rand = new Random();
public Consumer(Ringpuffer buffer, boolean wait)
{
this.buffer = buffer;
this.wait = wait;
}
public void run()
{
for(int i=0; i<=50; i++)
{
System.out.print("\n-Consumer: "+buffer.lies());
if(wait)
try
{
this.sleep(rand.nextInt(max_thread_sleep));
}
catch(InterruptedException e)
{
System.err.print("\nConsumer: Fehler bei Thread.sleep\n"+e);
}
}
}
}
forget about the thread, got the mistake finally =)