简体   繁体   English

线程同步

[英]Thread Synchronization

I have 2 threads in my program as follows: 我的程序中有2个线程,如下所示:

class SendThread implements Runnable {
     public void run(){
           Thread.sleep(1000);

           while (true){    
                if (CONNECTION_ESTABLISHED){
                    // code here
                }
           } 
     } 
 } 

class ReceiveThread implements Runnable {
     public void run(){


           while (true){    
                // code here
                CONNECTION_ESTABLISHED = true;  

           }
     }
 }

I have defined CONNECTION_ESTABLISHED as a static Boolean . 我已将CONNECTION_ESTABLISHED定义为static Boolean

In the second thread, the Boolean CONNECTION_ESTABLISHED is set to true at a certain point. 在第二个线程中, Boolean CONNECTION_ESTABLISHED在特定点设置为true If I don't use Thread.sleep(1000) in the 1st thread, after CONNECTION_ESTABLISHED is set to true in the 2nd thread I don't enter the relevant if statement in the 1st thread. 如果我没有在第一个线程中使用Thread.sleep(1000) ,那么在第二个线程中将CONNECTION_ESTABLISHED设置为true之后,我就不会在第一个线程中输入相关的if语句。

Is there another way around this? 还有其他解决方法吗? Because my 1st thread will often be dependent on variable changes in the second thread. 因为我的第一个线程通常将依赖于第二个线程中的变量更改。

volatile关键字添加到CONNECTION_ESTABLISHED方法中,然后查看。

You are facing teh challenge because you are changing the static variable CONNECTION_ESTABLISHED in a non-synchronized way and you are not using the wait notify mechanism for thread communication. 您正面临挑战,因为您正在以非同步方式更改静态变量CONNECTION_ESTABLISHED,并且没有将等待通知机制用于线程通信。 Using wait notify should help you. 使用等待通知应该可以为您提供帮助。 Create an object to be used for wait-notify and use the code as below 创建一个用于等待通知的对象,并使用以下代码

class SendThread implements Runnable {
     public void run(){
           Thread.sleep(1000);

           while (true){  
                synchronized(object)  {
                    object.wait();
                    if (CONNECTION_ESTABLISHED){
                       // code here

                    }
                }
           } 
     } 
 } 

class ReceiveThread implements Runnable {
     public void run(){


           while (true){   
                synchronized(object)  {
                   // code here
                   CONNECTION_ESTABLISHED = true;  
                   object.notify(); 
                  }
           }
     }
 }

I have not tested the code, i have just tried to put the concept so make changes as required. 我没有测试代码,我只是尝试提出概念,因此可以根据需要进行更改。

In this particular example you can mark the CONNECTION_ESTABLISHED boolean as volatile and it will work as expected, other threads will see the change the next time they access the variable. 在此特定示例中,您可以将CONNECTION_ESTABLISHED布尔值标记为volatile,它将按预期工作,其他线程在下次访问该变量时将看到更改。

However I will warn you against using volatile for anything except the most simple cases (single variable update). 但是,我警告您,除了最简单的情况(单变量更新)以外,不要对任何其他东西使用volatile。 It provides no atomicity so if you need to do more than a single assignment during an update it will not provided the necessary guarantees. 它没有原子性,因此,如果您在更新过程中需要执行的任务不止一次,则不会提供必要的保证。 This includes increments/decrements which are actually 3 operations, read/modify/write at the memory model level so requires proper synchronization. 这包括增量/减量,实际上是3个操作,在内存模型级别读取/修改/写入,因此需要适当的同步。

In that case you should use a synchronized block around both the read AND write. 在这种情况下,您应该在读取和写入周围使用同步块。

private final Object lock = new Object();

private int shared1;
private int shared2;

public void write(int var1, int var2)
{
  sychronized (lock)
  {
    shared1 = var1;
    shared2 = var2;
  }
}

public int readSum()
{
  sychronized (lock)
  {
    int total = shared1 + shared2;
    return total;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM