创建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...................
我想得到的结果是:
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...................
解决方案 »
- 一个关于实现多个接口的奇怪问题
- 在线等!!!求这段js函数的java代码!!!求解,谢谢大家了
- JSP中怎样把变量值输出到text文本框啊?帮帮忙
- java小白提问,Run As Ant Build 没有输入输出吗?
- 保存大图片时出错“java.lang.OutOfMemoryError: Java heap space” 高分求助!
- 关于配置文件的问题
- 关于hibernate查询问题????
- 请问JSP和JAVA是什么关系?
- spring ehcache手动或定时刷新缓存,如何实现?
- 大佬们求助
- JAVA 简单报表打印
- 急呀,我用eclipse开发第一个servlet出现了问题,搞了好久都不行
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.");
}}
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.");
}}