我试图运行这三个同时的线程,但是当我做system.print Char不会出来,并且计数器“我”超过了绑定。
不知何故,我在Char前面添加一个字符串,它打印出来正确,任何人都可以向我解释为什么会发生这种情况?
public class Part2 { public static void main(String[] args) { Runnable printA = new PrintChar('a'); Runnable printB = new PrintChar('b'); Runnable printC = new PrintChar('c'); Thread t1 = new Thread(printA); Thread t2 = new Thread(printB); Thread t3 = new Thread(printC); t1.start(); t2.start(); t3.start(); } private static class PrintChar implements Runnable { private char c; public PrintChar(char c) { this.c = c; } public void run() { for(int i = 1; i<=100; i++) { System.out.print(c + i + ", "); } } } }
/ *这是此代码的输出:98,100,101,102,103,104,105,99,99,...... 198,* /
/ *如果我在Char之前添加一个String,就像这样这是我期望的输出; * /
public void run() { for(int i = 1; i<=100; i++) { System.out.print("" + c + i + ", "); } }
/ * b1,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,.... a1~a100 b1~b100和c1~c100同时运行和完成* /
临摹微笑
米脂
相关分类