在下面的代碼中,線程t1和t2屬於同一個對象嗎?如是?哪個對象。他們會競爭獲得顯示器或互斥鎖嗎?來自Java中主類的兩個內聯線程屬於同一個對象?
如果它們屬於不同的對象。怎麼樣?對監視器的解釋是有用的。
package com.java.thread.practice;
public class InlineObject_SameThread {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable(){
@Override
public void run() {
// Will this compete with thread t2 to acquire monitor ??
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
// Will this compete with thread t1 to acquire monitor ??
}
});
// Starting the first thread.
t1.start();
// Starting the second thread.
t2.start();
/* There can be further logic, currently I will placing those as
* we progress.
*/
}
/*
Code ends.
*/
}
線程(或一般的對象)不屬於其他對象。其他對象可能對這些線程有*引用*,但這不是所有權,不僅僅是知道我的電話號碼擁有我的手機的人。 –
我從你的帖子中刪除_「我正在尋找討論」_。 StackOverflow顯然不適用於討論。另外,請注意我所做的其他格式更改,並瞭解如何格式化您的帖子和代碼。 –
謝謝@AndyTurner。 這是否意味着這兩個線程引用相同的對象'this(InlineObject_SameThread)'? –