创建4个线程,对同一变量进行计数操作,并把线程ID号和变量的值写到A.TEXT中,如果变量的值达到50w,则线程终止,并提示用户线程结束。
我想得到的结果是:
X  1
X  2
...按顺序输出,没有隔过去的数,也没有一样的数字。我写了一个,但是输出的结果和我想的不一样。public class NewTest extends Thread {
public final static int NUM = 5000;
public static int count = 0;
private PrintWriter writer = null; public NewTest(String threadName) {
super(threadName);
} @Override
public void run() {
synchronized (this) {

while (true) {
if (count >= NewTest.NUM)
return;
count++;
try {
writer = new PrintWriter(new FileWriter(new File("A.txt"), true), true);
} catch (IOException e1) {
e1.printStackTrace();
}
writer.println(this.getName() + "\t" + count);
writer.close();
}
} } public static void main(String[] args) {
Thread a = new NewTest("A");
Thread b = new NewTest("B");
Thread c = new NewTest("C");
Thread d = new NewTest("D"); a.start();
b.start();
c.start();
d.start(); try {
a.join();
b.join();
c.join();
d.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Over.");
}}
输出的结果是:
A 1
A 2
A 3
B 5
C 7
D 7
C 9
B 10
C 11
B 12
C 12
B 14
C 15
C 17
C 18
D 17
C 19...................

解决方案 »

  1.   

    synchronized (this) {  // this 不是共享变量,不能同步,加个共享变量lock就好了
    public class NewTest extends Thread {
        public final static int NUM = 5000;
        public static int count = 0;
        private static byte[] lock = new byte[0];
        
        private PrintWriter writer = null;    public NewTest(String threadName) {
            super(threadName);
        }    @Override
        public void run() {
            synchronized (lock) {
                
                while (true) {
                    if (count >= NewTest.NUM)
                        return;
                    count++;
                    try {
                        writer = new PrintWriter(new FileWriter(new File("D:/A.txt"), true), true);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    writer.println(this.getName() + "\t" + count);
                    writer.close();
                }
            }    }    public static void main(String[] args) {
            Thread a = new NewTest("A");
            Thread b = new NewTest("B");
            Thread c = new NewTest("C");
            Thread d = new NewTest("D");        a.start();
            b.start();
            c.start();
            d.start();        try {
                a.join();
                b.join();
                c.join();
                d.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Over.");
        }}
      

  2.   

    LS已经说了,this是当前对象,每个线程锁当前对象就是锁线程自己本身,达不到锁共同对象的目的,所以改成LS的,锁共同对象就可以了
      

  3.   

    楼主想要的应该是这种结果:
    A 1
    D 2
    D 3
    D 4
    D 5
    C 6
    ...
    应该把while放在synchronized的外面,锁对象换成2楼说的,代码如下:
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;public class NewTest extends Thread {
    public final static int NUM = 5000;
    public static int count = 0;
    private static byte[] lock = new byte[0]; private PrintWriter writer = null; public NewTest(String threadName) {
    super(threadName);
    } @Override
    public void run() {
    while (true) {
    synchronized (lock) {
    if (count >= NewTest.NUM)
    return;
    count++;
    try {
    writer = new PrintWriter(new FileWriter(
    new File("D:/A.txt"), true), true);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    writer.println(this.getName() + "\t" + count); }
    } } public static void main(String[] args) {
    Thread a = new NewTest("A");
    Thread b = new NewTest("B");
    Thread c = new NewTest("C");
    Thread d = new NewTest("D"); a.start();
    b.start();
    c.start();
    d.start(); try {
    a.join();
    b.join();
    c.join();
    d.join();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("Over.");
    }}