Java 线程:是否有任何结果不能作为这些程序的输出? (给出代码)
上周我做了一个关于线程的测验,我答错了这两个问题。我想知道是否有人可以帮助我获得这些问题的正确答案。谢谢。
是否有任何结果不能作为该程序的输出? 我对这类问题感到困惑。我运行程序发现Bbccaa是可能的,aaccbb是可能的,ccbbaa是可能的,aabbcc是可能的。
public class Test4 extends Thread { //8
public Test4(String name) {
super(name);
}
public void run() {
print(getName());
}
public static synchronized void print(String n) {
System.out.print(n);
try { sleep(...); } catch (Exception ex) {} // unspecified random time
System.out.print(n);
}
public static void main(String argv[]) throws Exception {
Test4 ta = new Test4("a");
Test4 tb = new Test4("b");
Test4 tc = new Test4("c");
ta.start();
tb.start();
tc.start();
ta.join();
tb.join();
tc.join();
}
}
此代码有同样的问题 阿巴是可能的,巴布是可能的,巴巴是可能的。我对这类问题感到困惑。是否有任何技巧或提示可以帮助我了解哪些输出是不可能的。
public class Test3 extends Thread {
public Test3(String name) {
super(name);
}
public void run() {
print(getName());
}
public static void print(String n) {
System.out.print(n);
try { sleep(...); } catch (Exception ex) {}
System.out.print(n);
}
public static void main(String argv[]) throws Exception {
Test3 ta = new Test3("a");
Test3 tb = new Test3("b");
Test3 tc = new Test3("c");
ta.start();
tb.start();
ta.join();
tb.join();
}
}
I had a quiz for Threads last week and I got these 2 questions wrong. I was wondering if anyone can help me get the right answer for these. Thanks.
Are there any results that CANNOT be the output of this program?
I get confused with these sort of questions. I ran the program and found that Bbccaa is possible, aaccbb is possible, ccbbaa is possible and aabbcc is possible.
public class Test4 extends Thread { //8
public Test4(String name) {
super(name);
}
public void run() {
print(getName());
}
public static synchronized void print(String n) {
System.out.print(n);
try { sleep(...); } catch (Exception ex) {} // unspecified random time
System.out.print(n);
}
public static void main(String argv[]) throws Exception {
Test4 ta = new Test4("a");
Test4 tb = new Test4("b");
Test4 tc = new Test4("c");
ta.start();
tb.start();
tc.start();
ta.join();
tb.join();
tc.join();
}
}
Same question for this code
Abab is possible, baab is possible, baba is possible. I get confused with these sort of questions. Is there any tricks or tips that will help me understand which output is not possible.
public class Test3 extends Thread {
public Test3(String name) {
super(name);
}
public void run() {
print(getName());
}
public static void print(String n) {
System.out.print(n);
try { sleep(...); } catch (Exception ex) {}
System.out.print(n);
}
public static void main(String argv[]) throws Exception {
Test3 ta = new Test3("a");
Test3 tb = new Test3("b");
Test3 tc = new Test3("c");
ta.start();
tb.start();
ta.join();
tb.join();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键点是方法签名中的synchronized关键字。这就是为什么你总是在第一个字母中连续出现两个字母(例如
cc
)。如果没有这个(第二个),唯一的限制是a
和b
恰好打印两次(按某种顺序),并且不打印其他字符。运行该程序对于测试线程没有太大帮助。你必须证明为什么它是正确的。
The key point is the synchronized keyword in the method signature. That's why you always have the two letters consecutively in the first (e.g.
cc
). Without that (the second), the only constraint is thata
andb
are printed exactly twice (in some order), and no other characters are printed.Running the program is not that helpful for testing threading. You have to demonstrate why it's correct.
马修说的第一点是对的。因此,只要它是连续的,它就可以具有类似 aabbcc 或 ccbbaa 的任何内容,因此有“同步”一词。
第二个我认为代码不会打印出 aacc。告诉我我是否错了
Matthew is right for the first. So it can have anything like aabbcc or ccbbaa as long as it is consecutive, hence the
synchronized
term.The second one I think the code wont print out aacc. Tell me if I am wrong