[英]java thread synchronization issue
Would like to post some doubts regarding Thread synchronization. 想对线程同步发表一些疑问。 I understood the concepts of synchronization. 我了解同步的概念。 But when I implemented a sample java program through multithreading, I could not achieve the result what I want. 但是,当我通过多线程实现示例Java程序时,无法达到我想要的结果。 But finally I could achieve the result by changing one line of code.But I would like to know whats wrong with the below code. 但是最终我可以通过更改一行代码来实现结果。但是我想知道以下代码有什么问题。
Here when I use synchronized block, synchnonized(SharedResource.class) I could achieve the result what I want. 在这里,当我使用同步块,synchnonized(SharedResource.class)时,可以达到我想要的结果。 why it is not working for synchonized method and synchronized(this) block? 为什么它不适用于同步方法和synced(this)块?
public class SharedResource {
public synchronized void access(String name){
System.out.println(name+" :accessed shared resoure");
System.out.println(name+" doing his job: ");
for(int i = 0; i < 5;i++){
try {
System.out.println(name+": "+ i);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name+" :finished doing his job..");
}
}
public class SharedAccessThread implements Runnable {
private String name ;
public SharedAccessThread(String name) {
this.name = name ;
}
@Override
public void run() {
SharedResource resource = new SharedResource();
resource.access(Thread.currentThread().getName());
}
}
public class MultiThreading {
public static void main(String[] args) {
SharedAccessThread thread = new SharedAccessThread(Thread.currentThread().getName());
Thread t1 = new Thread(thread);
t1.setName("A");
Thread t2 = new Thread(thread);
t2.setName("b");
Thread t3 = new Thread(thread);
t3.setName("C");
t1.start();
t2.start();
t3.start();
}
}
The expected out put is below:
A :accessed shared resoure
A doing his job:
A: 0
A: 1
A: 2
A: 3
A: 4
A :finished doing his job..
C :accessed shared resoure
C doing his job:
C: 0
C: 1
C: 2
C: 3
C: 4
C :finished doing his job..
b :accessed shared resoure
b doing his job:
b: 0
b: 1
b: 2
b: 3
b: 4
b :finished doing his job..
But it gives in a shuffled manner. 但是它以混乱的方式给出。 That means A accessed shared resource..B doing his job...,etc which I dont want. 这意味着A可以访问共享资源。B可以完成我不想要的工作...等等。
The problem is that you are creating a new SharedResource
inside each thread: 问题是您正在每个线程内创建一个new SharedResource
:
@Override
public void run() {
SharedResource resource = new SharedResource();
resource.access(Thread.currentThread().getName());
}
Therefore, synchronizing on this
or on the method itself (which is the same as synchronizing on this
) causes each thread to see a different lock and so it has no effect. 因此,在同步this
或在方法本身(这是相同的上同步this
)导致每个线程看到一个不同的锁,所以它没有效果。
Synchronizing on the class itself works because it functions as a global lock, since the class itself is the same for all threads. 由于类本身对所有线程都是相同的,因此在类本身上进行同步是因为它充当全局锁,因此可以进行同步。
To be able to synchronize on this
you should make the resource a static member for example: 为了能够this
进行同步,您应该使资源成为静态成员,例如:
public class SharedAccessThread implements Runnable {
private String name;
private static SharedResource resource = new SharedResource();
public SharedAccessThread(String name) {
this.name = name ;
}
@Override
public void run() {
resource.access(Thread.currentThread().getName());
}
}
This guarantees that all your threads see the same instance of SharedResource
. 这样可以确保您的所有线程都看到相同的SharedResource
实例。
Your code not need to synchronization. 您的代码不需要同步。 Synchronization important in positions such as : Global member and any thread change its values
, but in your sample dont any mutual exclusion. 同步在以下位置很重要: Global member and any thread change its values
,但是在示例中不要进行任何互斥。 In java any thread has own Thread Context
consist of local variable
, input parameter
and etc. In your code, any thread has a SharedResource
instance and call access
method of it, so don't worry for thread-safety
issue. 在Java中,任何线程都有自己的Thread Context
,该Thread Context
由local variable
, input parameter
等组成。在您的代码中,任何线程都有SharedResource
实例并调用其access
方法,因此不必担心thread-safety
问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.