# Java threads



## caleb (Dec 7, 2010)

How do I make sure a block of code gets executed completly on a shared memory resource within a thread ? I wouldnt like any other thread thats running operations on array 'paleczki' to be able to change that particular value on that particular adress like 

```
if(a == 0) 
         {
             if(paleczki[lewa] == 0)
             {
                 paleczki[lewa]=1;
                 if(paleczki[prawa] == 0)
                    {
                    paleczki[prawa]=1;
                    //jem!
                    paleczki[lewa]=0;
                    paleczki[prawa]=0;
                   }else{
                    paleczki[lewa]=0;
                   }
                
             }
```


----------



## streetfighter 2 (Dec 7, 2010)

You could use a mutex?
http://java.sun.com/developer/technicalArticles/J2SE/concurrency/


----------



## caleb (Dec 7, 2010)

Hmm I actually put all my code into a class and then ran it from run(). It appears to run properly ?!


----------



## streetfighter 2 (Dec 7, 2010)

caleb said:


> Hmm I actually put all my code into a class and then ran it from run(). It appears to run properly ?!


Do you mean that you did that without implementing any thread synchronization tools?

If you had two threads that were looping access and modification of a shared resource then the possibility of a synchronization problem would be high.  If the function is only looped once then the possibility of a synchronization problem is low but not zero.  The more often that threads modify a shared resource the higher the probability of a problem becomes.


----------



## caleb (Dec 7, 2010)

Im trying to do this 
http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MUTEX/TM-example-philos-1.html


----------



## streetfighter 2 (Dec 7, 2010)

The dining philosophers eh?

Classic.

I wrote a solution to that in C using spinlocks way back in the day.

You know that wikipedia has a solution to the dining philosophers problem in Java (second solution) using semaphores?


----------



## caleb (Dec 7, 2010)

Think I did it in sorta a noob way but it looks like it's working with the shuffle on stick pickup and then attempt to pickup another. Not sure if Java manages the memory as I think it does tho but the output seems like its working cause the eaten times are close to each other but vary a bit and in the end one or two threads seem to catchup to the 10k loops.


```
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package javaapplication1;
import java.util.*;

/**
 *
 * @author 
 */
class losuj_paleczke extends Thread{
    
  
    Random generator = new Random();
    int a = generator.nextInt(2);
    int paleczki[];
    int lewa,prawa;
    int jadlem = 0;
    int id_filozofa;
    public losuj_paleczke(int paleczki_lok[],int a,int b,int c){
        paleczki = paleczki_lok;
        lewa = a;
        prawa = b;
        id_filozofa = c;
     }

    public int eat(int jadl)
    {
     if(a == 0)
         {
             if(paleczki[lewa] == 0)
             {
                 paleczki[lewa]=1;
                 if(paleczki[prawa] == 0)
                    {
                    paleczki[prawa]=1;
                    //jem!
                    jadl++;
                    paleczki[lewa]=0;
                    paleczki[prawa]=0;
                   }else{
                    paleczki[lewa]=0;
                   }

             }

         }else{
              if(paleczki[prawa] == 0)
             {
                 paleczki[prawa]=1;
                 if(paleczki[lewa] == 0)
                    {
                    paleczki[lewa]=1;
                    //jem!
                    jadl++;
                    paleczki[prawa]=0;
                    paleczki[lewa]=0;
                   }else{
                    paleczki[lewa]=0;
                   }

             }

         }
     return jadl;
 }

 public void run(){
     try{
    for (int i = 0; i < 10000; i++) {
      // Filozof mysli
         Thread.sleep(2);
      // Filozof je
      jadlem = eat(jadlem);

  System.out.println("Filozof numer "+id_filozofa+" Jadł :"+ jadlem);

   
 }
  
 }catch (Exception e){
    }
     }
}

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int[] paleczki_raw = {0, 0, 0, 0, 0, 0, 0};
       losuj_paleczke w1 = new losuj_paleczke(paleczki_raw,0,1,1);
       losuj_paleczke w2 = new losuj_paleczke(paleczki_raw,1,2,2);
       losuj_paleczke w3 = new losuj_paleczke(paleczki_raw,2,3,3);
       losuj_paleczke w4 = new losuj_paleczke(paleczki_raw,3,4,4);
       losuj_paleczke w5 = new losuj_paleczke(paleczki_raw,4,5,5);
       losuj_paleczke w6 = new losuj_paleczke(paleczki_raw,5,6,6);
       losuj_paleczke w7 = new losuj_paleczke(paleczki_raw,6,0,7);
        w1.start();
        w2.start();
        w3.start();
        w4.start();
        w5.start();
        w6.start();
        w7.start();

    }

}
```


----------



## caleb (Dec 8, 2010)

I think I got it.I subbed all the array operations into semaphore ones. Kinda cool how randomizing the stick pickup can solve thread concurrency.


----------

