A
A
Alexander Sorokin2017-03-24 15:58:00
Java
Alexander Sorokin, 2017-03-24 15:58:00

In different classes at the same time, the same variable gives different values, what to do?

Good afternoon!
The TimeCheck class has a play variable, a setter and a getter for it. The value of play is displayed here.

package AppPackage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

  public class TimeCheck implements Runnable{
        MainClass MC = new MainClass();    
        private final Object lock = new Object();
        private boolean flag;
        private boolean stop;
        private boolean play;
        boolean alive;
        String time;
        
         public TimeCheck() {
             super();
        }
        
        Thread t;
        public TimeCheck(String aTime) {
           t = new Thread(this);
           time = aTime;
        }

        @Override
        public void run() {
            System.out.println("Вошли в " + t.getName());
            synchronized(lock) {
                while(!stop) {
                    try {
                        if(flag) lock.wait();   

                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
                        if ( time.equals(sdf.format(date)) && !play) {
 /*----------->>>*/  System.out.println(t.getName() + " -> play = " + getIsPlaying());
                            System.out.println(t.getName() + " -> Звоню");
                            MC.Play("E:\\JP\\RAZNOE 2\\SimpleAlarm\\src\\AppPackage\\doom.mp3");
                        } else {
                            System.out.println("Время будильника не равно текущему " + sdf.format(date));
                        }
                        Thread.sleep(1000);
                    } 
                    catch (InterruptedException ex) 
                    {
                    }
                }
            }
                
        }
        public void playerStop() {                                   
            MC.Stop();
        }     
        public boolean getIsPlaying() {
            return play;
        }
        
        public void setIsPlaying() {
            play = true;
        }
        
        public void pauseThread() {
            flag = true;
            System.out.println("Зашли в паузу в " + t.getName());
        }

        public void resumeThread() {
            flag = false;
            synchronized(lock) {
                lock.notify();   
            }
        }
        public void stopThread() {
            stop = true;   // завершает поток естественно
            System.out.println("Зашли в стоп.");
        }
}

There is a MainClass class. Here, the play setter is set to true and displayed.
package AppPackage;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class MainClass 
{
    
    FileInputStream FIS;
    BufferedInputStream BIS;
    
    
    public Player player;
    
    public boolean selected;
                
    public long pauseLocation;
    public long songTotalLenght;
    
     
    public void Stop() {
        if (player != null) {
            player.close();
            pauseLocation = 0;
            songTotalLenght = 0;
            
            
        }
    }
    
    public void Play(String path) {
        try {
            FIS = new FileInputStream(path);
            BIS = new BufferedInputStream(FIS);
            
            player = new Player(BIS);
            songTotalLenght = FIS.available();
            
            TimeCheck TC = new TimeCheck();
/*---->>>*/       TC.setIsPlaying();
/*---->>>*/       System.out.println("play в MainClass = " + TC.getIsPlaying());</b>
        } 
        catch (FileNotFoundException | JavaLayerException ex) 
        {
           
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        new Thread(){
            @Override
            public void run(){
                try {
                    player.play();
                } 
                catch (JavaLayerException ex) 
                {
                
                }
            }
        }.start();  
    }
    
}

This is what
run outputs to the console:
We entered Thread-1
Thread-1 -> play = false
Thread-1 -> I
call play in MainClass = true
Thread-1 -> play = false
Thread-1 -> I
call play in MainClass = true
Thread-1 -> play = false
Thread-1 -> I
call play in MainClass = true

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Oparin, 2017-03-24
@losse_narmo

5 things you didn't know about multithreading - the third point

A
Axian Ltd., 2017-03-24
@AxianLTD

Why aren't getter and setter synchronized?

A
Alexander Sorokin, 2017-03-27
@loveangel

Thanks to all!
Marked the play variable as volatile, and at the same time, out of desperation, marked the setter and getter for it as synchronized. Did not help.
As a result, I marked the play variable as static.
Helped)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question