创建k个线程,同时打入一个文件符号。在0行的文件中第一个线程写入数字0 .........在9行的文件中十个线程写入数字9。
实施要求。
备注。
我认为解决了我的问题,这是RandomAccessFile中的seek方法,但是我不知道他的用法。
public final class Part5 {
private static final List<Thread> threadList = new ArrayList<>();
private static AtomicInteger pos = new AtomicInteger(0);
private static final String FILE_NAME = "part5.txt";
private Part5() {
}
public static void main(String[] args) throws InterruptedException {
run();
}
public static void run() throws InterruptedException {
try {
final RandomAccessFile accessFile = new RandomAccessFile(FILE_NAME, "rw");
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RandomThread(accessFile, new AtomicInteger(i), pos));
threadList.add(thread);
thread.start();
pos.getAndAdd(20);
}
for (Thread thread : threadList) {
thread.join();
}
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
class RandomThread implements Runnable {
private final RandomAccessFile accessFile;
private AtomicInteger number;
private AtomicInteger pos;
public RandomThread(RandomAccessFile accessFile, AtomicInteger number, AtomicInteger pos) {
this.accessFile = accessFile;
this.number = number;
this.pos = pos;
}
@Override
public synchronized void run() {
synchronized (accessFile) {
try {
for (int i = 0; i < 20; i++) {
// accessFile.seek(pos.getAndIncrement());
accessFile.writeBytes(number.toString());
Thread.sleep(1);
}
accessFile.write(System.lineSeparator().getBytes());
Thread.currentThread().interrupt();
} catch (IOException | InterruptedException e) {
System.err.println(e.getMessage());
}
}
}
}
public final class Part5 {
private static final List<Thread> threadList = new ArrayList<>();
private static AtomicInteger pos = new AtomicInteger(0);
private static final String FILE_NAME = "part5.txt";
private Part5() {
}
public static void main(String[] args) throws InterruptedException {
run();
}
public static void run() throws InterruptedException {
try {
final RandomAccessFile accessFile = new RandomAccessFile(FILE_NAME, "rw");
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RandomThread(accessFile, new AtomicInteger(i), pos));
threadList.add(thread);
thread.start();
pos.getAndAdd(20);
}
for (Thread thread : threadList) {
thread.join();
}
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
class RandomThread implements Runnable {
private final RandomAccessFile accessFile;
private AtomicInteger number;
private AtomicInteger pos;
public RandomThread(RandomAccessFile accessFile, AtomicInteger number, AtomicInteger pos) {
this.accessFile = accessFile;
this.number = number;
this.pos = pos;
}
@Override
public synchronized void run() {
synchronized (accessFile) {
try {
for (int i = 0; i < 20; i++) {
// accessFile.seek(pos.getAndIncrement());
accessFile.writeBytes(number.toString());
Thread.sleep(1);
}
accessFile.write(System.lineSeparator().getBytes());
Thread.currentThread().interrupt();
} catch (IOException | InterruptedException e) {
System.err.println(e.getMessage());
}
}
}
}
文件输出
00000000000000000000
99999999999999999999
88888888888888888888
77777777777777777777
66666666666666666666
55555555555555555555
44444444444444444444
33333333333333333333
22222222222222222222
11111111111111111111
但是我等着
00000000000000000000
11111111111111111111
22222222222222222222
33333333333333333333
44444444444444444444
55555555555555555555
66666666666666666666
77777777777777777777
88888888888888888888
99999999999999999999