
获得徽章 0
赞了这篇文章
赞了这篇文章
public class TestForGuardedObject {
public static void main(String[] args) {
GuardedObject guardedObject = new GuardedObject();
new Thread(()->{
Object o = guardedObject.get();
System.out.println(o);
},"t1").start();
new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
guardedObject.set("张楚");
System.out.println("我是张楚,我已进来");
},"t2").start();
}
}
class GuardedObject{
private Object response;
public Object get(){
while(this.response == null){
System.out.println("GuardedObject中:" + response);
}
return response;
}
public void set(Object response){
this.response = response;
}
}
有没有佬帮忙看一下这段代码,为啥我删除get方法中的System.out就会卡死,不删除就能正常生产和消费
public static void main(String[] args) {
GuardedObject guardedObject = new GuardedObject();
new Thread(()->{
Object o = guardedObject.get();
System.out.println(o);
},"t1").start();
new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
guardedObject.set("张楚");
System.out.println("我是张楚,我已进来");
},"t2").start();
}
}
class GuardedObject{
private Object response;
public Object get(){
while(this.response == null){
System.out.println("GuardedObject中:" + response);
}
return response;
}
public void set(Object response){
this.response = response;
}
}
有没有佬帮忙看一下这段代码,为啥我删除get方法中的System.out就会卡死,不删除就能正常生产和消费
展开
评论
点赞
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
赞了这篇文章
public static void main(String[] args) {
// String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab"
String s1 = new String("a") + new String("b");////执行完以后,不会在字符串常量池中会生成"ab"
String s = s1.intern();
String s2 = "ab";
System.out.println(s1 == s2);//true
System.out.println(s == s2);//true
}
public static void main(String[] args) {
// String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab"
String s1 = new String("a") + new String("b");////执行完以后,不会在字符串常量池中会生成"ab"
String s2 = "ab";
String s = s1.intern();
System.out.println(s1 == s2);//false
System.out.println(s == s2);.//true
}
有大佬知道为什么这两个代码结果不一样是为啥吗?为什么交换s和s2的声明之后就不一样了
// String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab"
String s1 = new String("a") + new String("b");////执行完以后,不会在字符串常量池中会生成"ab"
String s = s1.intern();
String s2 = "ab";
System.out.println(s1 == s2);//true
System.out.println(s == s2);//true
}
public static void main(String[] args) {
// String s1 = new String("ab");//执行完以后,会在字符串常量池中会生成"ab"
String s1 = new String("a") + new String("b");////执行完以后,不会在字符串常量池中会生成"ab"
String s2 = "ab";
String s = s1.intern();
System.out.println(s1 == s2);//false
System.out.println(s == s2);.//true
}
有大佬知道为什么这两个代码结果不一样是为啥吗?为什么交换s和s2的声明之后就不一样了
展开
5
1