• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Java threads

Joined
Sep 15, 2004
Messages
1,587 (0.21/day)
Location
Poland,Slask
System Name HAL
Processor Core i5 2500K
Motherboard Asus P8P67 Pro Rev3.1
Cooling stock
Memory 2x4GB Kingston 1600Mhz Blu
Video Card(s) Asus 560Ti DirectCuII TOP
Storage Kingston 120 3K SSD,WD Black WD1502FAEX
Display(s) LG 1440x900
Case Chieftec Mesh Midi
Audio Device(s) onboard
Power Supply Corsair TX750V2
Software w8
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
Code:
 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;
                   }
                
             }
 
Hmm I actually put all my code into a class and then ran it from run(). It appears to run properly ?!
 
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.
 
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.

Code:
/*
 * 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();

    }

}
 
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.
 
Back
Top