-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAtomic.java
More file actions
29 lines (27 loc) · 899 Bytes
/
TestAtomic.java
File metadata and controls
29 lines (27 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package Thread;
import java.util.concurrent.atomic.AtomicReference;
public class TestAtomic {
public final static AtomicReference<String> atomicString = new AtomicReference<String>("hosee");
public static void main(String[] args)
{
for (int i = 0; i < 10; i++)
{
new Thread(() -> {
try
{
Thread.sleep(Math.abs((int)Math.random()*100));
}
catch (Exception e)
{
e.printStackTrace();
}
if (atomicString.compareAndSet("hosee", "ztk"))
{
System.out.println(Thread.currentThread().getId() + "Change value");
}else {
System.out.println(Thread.currentThread().getId() + "Failed");
}
}).start();
}
}
}