无法借助 Java 中的同步方法获得答案
Not able to get the answer with the help of synchronized method in Java
我必须在 Java 中使用多线程从文件 F1 和 F2 读取内容并将其移动到新文件 (f3)。我面临的问题是,当我在方法中使用同步时,我无法得到答案,但是当我不同步方法时,我能够得到答案。
下面是同步方法的代码,只打印一个文件的内容:
package com.company;
import java.io.*;
import java.io.FileReader;
public class FileMerge{
public static void main(String[] args) throws IOException, InterruptedException {
WriteToFile pc = new WriteToFile();
//Create a Thread1 to read the content of file 1
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.file1();
} catch (InterruptedException | IOException e) {
System.out.println(e);
}
}
});
// Create Thread 2 to read the content of file 2
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.file2();
} catch (InterruptedException | IOException e) {
System.out.println(e);
}
}
});
//Start the Thread
t1.start();
t2.start();
t1.join();
t2.join();
}
public static class WriteToFile{
PrintWriter pw = new PrintWriter("C:/Users/paras.jain/Desktop/des.txt");
public WriteToFile() throws FileNotFoundException {
}
public synchronized void file1() throws InterruptedException, IOException {
// BufferedReader object for file1.txt
BufferedReader br = new BufferedReader(new FileReader("C:\Users\paras.jain\Desktop\f.txt"));
String line = br.readLine();
//System.out.println("----------" + line);
// loop to copy each line of
// file1.txt to file3.txt
while (line != null) {
pw.println(line);
line = br.readLine();
}
br.close();
pw.close();
}
public synchronized void file2() throws InterruptedException, IOException {
BufferedReader br1 = new BufferedReader(new FileReader("C:/Users/paras.jain/Desktop/f2.txt"));
String line2 = br1.readLine();
//System.out.println("----------" + line2);
// loop to copy each line of
// file2.txt to file3.txt
while (line2 != null) {
pw.println(line2);
line2 = br1.readLine();
//System.out.println("----------" + line2);
}
br1.close();
pw.close();
}
}
}
以上代码的输出是:
File 1 line 1
File 1 line 2
File 1 line 3
当我 运行 代码 没有同步 方法时,我得到以下输出:
File 1 line 1
File 1 line 2
File 2 line 1
File 2 line 2
File 1 line 3
File 2 line 3
为什么输出会有所不同?以及如何借助 Synchronized 方法获得第二个输出?
当 file1()
和 file2()
方法同步时,其中一个必须先于另一个完成 运行。
由于您首先启动 运行 的 file1()
线程,它可能会赢得比赛,因此 file1()
必须在 file2()
可以 运行 之前完成.
不幸的是,file1()
关闭了 PrintWriter,所以当 file2()
运行 时,所有 pw.println(...)
调用 无声地失败.
为什么默默?因为那是他们定义它的方式。请参阅 PrintWriter
:
的 javadoc
Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError()
.
我必须在 Java 中使用多线程从文件 F1 和 F2 读取内容并将其移动到新文件 (f3)。我面临的问题是,当我在方法中使用同步时,我无法得到答案,但是当我不同步方法时,我能够得到答案。
下面是同步方法的代码,只打印一个文件的内容:
package com.company;
import java.io.*;
import java.io.FileReader;
public class FileMerge{
public static void main(String[] args) throws IOException, InterruptedException {
WriteToFile pc = new WriteToFile();
//Create a Thread1 to read the content of file 1
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.file1();
} catch (InterruptedException | IOException e) {
System.out.println(e);
}
}
});
// Create Thread 2 to read the content of file 2
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.file2();
} catch (InterruptedException | IOException e) {
System.out.println(e);
}
}
});
//Start the Thread
t1.start();
t2.start();
t1.join();
t2.join();
}
public static class WriteToFile{
PrintWriter pw = new PrintWriter("C:/Users/paras.jain/Desktop/des.txt");
public WriteToFile() throws FileNotFoundException {
}
public synchronized void file1() throws InterruptedException, IOException {
// BufferedReader object for file1.txt
BufferedReader br = new BufferedReader(new FileReader("C:\Users\paras.jain\Desktop\f.txt"));
String line = br.readLine();
//System.out.println("----------" + line);
// loop to copy each line of
// file1.txt to file3.txt
while (line != null) {
pw.println(line);
line = br.readLine();
}
br.close();
pw.close();
}
public synchronized void file2() throws InterruptedException, IOException {
BufferedReader br1 = new BufferedReader(new FileReader("C:/Users/paras.jain/Desktop/f2.txt"));
String line2 = br1.readLine();
//System.out.println("----------" + line2);
// loop to copy each line of
// file2.txt to file3.txt
while (line2 != null) {
pw.println(line2);
line2 = br1.readLine();
//System.out.println("----------" + line2);
}
br1.close();
pw.close();
}
}
}
以上代码的输出是:
File 1 line 1
File 1 line 2
File 1 line 3
当我 运行 代码 没有同步 方法时,我得到以下输出:
File 1 line 1
File 1 line 2
File 2 line 1
File 2 line 2
File 1 line 3
File 2 line 3
为什么输出会有所不同?以及如何借助 Synchronized 方法获得第二个输出?
当 file1()
和 file2()
方法同步时,其中一个必须先于另一个完成 运行。
由于您首先启动 运行 的 file1()
线程,它可能会赢得比赛,因此 file1()
必须在 file2()
可以 运行 之前完成.
不幸的是,file1()
关闭了 PrintWriter,所以当 file2()
运行 时,所有 pw.println(...)
调用 无声地失败.
为什么默默?因为那是他们定义它的方式。请参阅 PrintWriter
:
Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking
checkError()
.