线程连接与同步的单例对象或刺



System.out.println("hey got it...."(;从未在我的程序中执行过

我可以理解它,因为同步关键字,但实际上发生了什么。

从 CFG 类调用的 TestJoin 类,问题出在此类中

class TestJoin implements Runnable{
public String x;
TestJoin(String x){
this.x= x;
}
public void testJoin() {
System.out.println("b4"+x);
synchronized(x) {
System.out.println(x);
ThreadJoining t1 = new ThreadJoining("2");
// thread t1 starts
t1.start();
System.out.println("you fool");
try{
System.out.println(
"Current Thread: "+ 
Thread.currentThread().getName()
);
t1.join();
//never this line is executed
System.out.println("hey got it....");
}catch(Exception ex){
System.out.println(
"Exception has " +
"been caught" + ex
);
}
}
}
@Override
public void run() {
testJoin();
}
}

只需创建另一个从 TestJoin 类调用的线程

class ThreadJoining extends Thread{
String c;
ThreadJoining(String co){
c=co;
}
@Override
public void run()
{
synchronized(c) {
for (int i = 0; i < 2; i++){
try{
Thread.sleep(500);
System.out.println(
"Current Thread: " +
Thread.currentThread().getName()
);
}catch(Exception ex){
System.out.println(
"Exception has" +
" been caught" + ex
);
}
System.out.println(i);
}
}
}
}

主类执行从这里开始

public class GFG{
public static void main (String[] args){
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Bye Bye");
}
});
new Thread(new TestJoin("1")).start();
new Thread(new TestJoin("1")).start();
new Thread(new TestJoin("2")).start();
new Thread(new TestJoin("3")).start();
}
}

上述执行的输出

b41
1
b43
3
b42
2
b41
you fool
Current Thread: Thread-4
you fool
Current Thread: Thread-1
you fool
Current Thread: Thread-3

第二次编辑:

上述问题仅在我与字符串或单例对象同步时, 如果我使用一个新对象 它的完美文件

示例,使用普通对象同步

public class GFG{
public static void main (String[] args){
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Bye Bye");
}
});
Sample s =  new Sample();
new Thread(new TestJoin(s)).start();
new Thread(new TestJoin(s)).start();
new Thread(new TestJoin(new Sample())).start();
new Thread(new TestJoin(new Sample())).start();
}
}
public class TestJoin implements Runnable{
public Sample x;
TestJoin(Sample x){
this.x= x;
}
public void testJoin() {
System.out.println("b4"+x);
synchronized(x) {
System.out.println(x);
ThreadJoining t1 = new ThreadJoining("2");
// thread t1 starts
t1.start();
System.out.println("you fool");
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t1.join();
System.out.println("hey got it....");
}
catch(Exception ex)
{
System.out.println("Exception has " +
"been caught" + ex);
}
}
}
@Override
public void run() {
testJoin();
}
}
class ThreadJoining extends Thread{
String c;
ThreadJoining(String co){
c=co;
}
@Override
public void run(){
synchronized(c) {
for (int i = 0; i < 2; i++){
try{
Thread.sleep(500);
System.out.println(
"Current Thread: " +
Thread.currentThread().getName()
);
}catch(Exception ex){
System.out.println(
"Exception has" +
" been caught" + ex
);
}
System.out.println(i);
}
}
}
}
public class Sample {
}

输出

b4Sample@a5c4778
b4Sample@27efa2ad
b4Sample@a5c4778
b4Sample@27507837
Sample@27507837
Sample@27efa2ad
Sample@a5c4778
you fool
you fool
Current Thread: Thread-4
you fool
Current Thread: Thread-3
Current Thread: Thread-1
Current Thread: Thread-6
0
Current Thread: Thread-6
1
hey got it....
Current Thread: Thread-5
0
Current Thread: Thread-5
1
hey got it....
Current Thread: Thread-7
0
Current Thread: Thread-7
1
hey got it....
Sample@a5c4778
you fool
Current Thread: Thread-2
Current Thread: Thread-8
0
Current Thread: Thread-8
1
hey got it....
Bye Bye

这是因为字符串常量被扣留,所以你在ThreadJoiningTestJoin中同步"2",你实际上是锁定在同一个对象上。

new TestJoin("2")锁定"2"然后等待new ThreadJoining("2")完成时,这会导致死锁(这永远不会发生,因为它无法"2"锁定(。

最新更新