Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problems synchronizing Threads  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2004-05-03 17:47:43 »

i have to go into thread synchronizing and have therefore
Offline Serethos

Junior Member




Java games rock!


« Reply #1 - Posted 2004-05-03 17:52:17 »

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)
      {
      //      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);
                  }
            }
      }
}
Offline Serethos

Junior Member




Java games rock!


« Reply #2 - Posted 2004-05-04 11:28:37 »

forget about the thread, got the mistake finally =)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (63 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!