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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| 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) { { 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(); } } public synchronized Integer lies() { { 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); 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); } } } } |