Whether thread safety is related to whether it is used in multiple threads
Although what you defined is private, there are many ways to access it indirectly in other threads, so it is possible to use it in multiple threads, but there is no synchronization processing added to the code, so it is not safe.
Supplement
There is no difference between using Thread and Runnable:
public class Test {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
// MyRunable mr = new MyRunable();
// new Thread(mr).start();
// new Thread(mr).start();
// new Thread(mr).start();
}
}
class MyThread extends Thread {
private int ticket = 10;
public void run() {
for (int i = 0; i < 20; i++) {
if (this.ticket > 0) {
System.out.println("thread: " + this.ticket--);
}
}
}
}
class MyRunable implements Runnable {
private int ticket = 10;
public void run() {
for (int i = 0; i < 20; i++) {
if (this.ticket > 0) {
System.out.println("runable: " + this.ticket--);
}
}
}
}
A running example without a case (you have to run it a few times to find it)
Every time a new object is created, the map inside it is owned by the object itself, so it is safe.
This sentence is correct, unless you declare a public member (variable) in the main thread that calls the sub-thread and operate this public variable inside the sub-thread, or you pass this public variable into the sub-thread by reference Inside the thread and operating it inside the sub-thread, this will lead to thread insecurity problems. As for whether the map type itself is thread-safe, I have forgotten it (I remember that map is an interface, whether it is thread-safe or not depends on it. For specific implementation), you can search it on Baidu. . .
If the implementation of map itself is thread-safe, then no matter how you operate inside multi-threads, it will be fine. (Even if it is declared in the main thread and passed by reference to the child thread)
For specific popular science knowledge about thread safety, you can read the articles I wrote before https://zhuanlan.zhihu.com/p/...
How to put it, it’s like: You put your money in your suitcase and walk down the street alone. You think it’s safe, of course. But once it is robbed, it is not safe. . .
Thread safety means that different threads access the same data. If there is only one thread, there is no thread safety. Or you can also understand it as "safe", after all, no other objects can access it, but it is not "thread safe"
Answer the question:
Is this map object thread-unsafe?
Yes, it is not thread-safe. Because although each Thread object here has a unique and independent Map object, it does not have "thread safety capabilities". Well, this is my understanding, it seems a bit verbose. . . ==
It mainly depends on whether you have accessed a certain public resource. This question does not involve accessing a certain public resource, so it cannot be said to be safe or unsafe.
Whether thread safety is related to whether it is used in multiple threads
Although what you defined is
private
, there are many ways to access it indirectly in other threads, so it is possible to use it in multiple threads, but there is no synchronization processing added to the code, so it is not safe.Supplement
There is no difference between using Thread and Runnable:
A running example without a case (you have to run it a few times to find it)
This sentence is correct, unless you declare a public member (variable) in the main thread that calls the sub-thread and operate this public variable inside the sub-thread, or you pass this public variable into the sub-thread by reference Inside the thread and operating it inside the sub-thread, this will lead to thread insecurity problems. As for whether the map type itself is thread-safe, I have forgotten it (I remember that map is an interface, whether it is thread-safe or not depends on it. For specific implementation), you can search it on Baidu. . .
If the implementation of map itself is thread-safe, then no matter how you operate inside multi-threads, it will be fine. (Even if it is declared in the main thread and passed by reference to the child thread)
For specific popular science knowledge about thread safety, you can read the articles I wrote before https://zhuanlan.zhihu.com/p/...
How to put it, it’s like:
You put your money in your suitcase and walk down the street alone.
You think it’s safe, of course.
But once it is robbed, it is not safe. . .
Thread safety means that different threads access the same data. If there is only one thread, there is no thread safety. Or you can also understand it as "safe", after all, no other objects can access it, but it is not "thread safe"
Answer the question:
Yes, it is not thread-safe.
Because although each Thread object here has a unique and independent Map object, it does not have "thread safety capabilities".
Well, this is my understanding, it seems a bit verbose. . . ==
Thanks for the invitation!
Limited usage in to
new MyThread().start()
的情况下是线程安全
.Although you declared it private, you can still read the variable in another thread. Without a synchronization lock, it is thread unsafe.
Reading is no problem, writing will cause thread safety issues. . .
1. Use thread-safe class methods
2. Use ThreadLocal
Think of
MyThread
只是看成一个类(别想它是一个线程类),把obj
just as a member of this class. Then it becomes easier to understand.In case of multi-threading
It mainly depends on whether you have accessed a certain public resource. This question does not involve accessing a certain public resource, so it cannot be said to be safe or unsafe.
It mainly depends on whether you have operated on this variable, and assuming that you come out with a new object every time, it is thread-safe.