如何在JAVA中使用多线程在不同的文件上写入数据?

如何在JAVA中使用多线程在不同的文件上写入数据?,java,multithreading,file,Java,Multithreading,File,我有一个多线程服务器。服务器接受来自不同客户端的连接。这些客户机定期发送数据。我的服务器有一个标准的多线程代码样式,如线程、运行方法等。但是,当我启动一个客户端时,它会发送数据,在此过程中,当我启动另一个客户端时,第一个线程停止,第二个线程开始将数据写入它的特定文件。换句话说 预期成果: 第一个线程的持续时间-->file1.txt中的10秒数据, 第二个线程的持续时间-->file2.txt中的10秒数据 实际结果: 第一个线程的持续时间--->文件1.txt中10秒数据中只有3秒(我在启动第

我有一个多线程服务器。服务器接受来自不同客户端的连接。这些客户机定期发送数据。我的服务器有一个标准的多线程代码样式,如线程、运行方法等。但是,当我启动一个客户端时,它会发送数据,在此过程中,当我启动另一个客户端时,第一个线程停止,第二个线程开始将数据写入它的特定文件。换句话说

预期成果:

第一个线程的持续时间-->file1.txt中的10秒数据, 第二个线程的持续时间-->file2.txt中的10秒数据

实际结果:

第一个线程的持续时间--->文件1.txt中10秒数据中只有3秒(我在启动第一个线程3秒后启动了第二个线程) 第二个线程的持续时间-->10秒file2.txt中的数据

反之亦然

我想使用多线程编写不同的多个文件

你能帮我解决这个问题吗

谢谢

这是我的密码:

    public void run() {
        PrintWriter outputStream = null;
        try {

            int x=0;
            while (runThread) {
                FileOutputStream output = new FileOutputStream(getRootDirectory() + fileName, true);
                for (int i = 17; i < requestData.length; i++) {
                    output.write(requestData[i]);
                }
                output.close();

                if (!ServerOn) {
                    System.out.print("Server has already stopped");
                    outputStream.println("Server has already stopped");
                    outputStream.flush();
                    m_bRunThread = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
                myClientSocket.close();
                System.out.println("...Stopped");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
public void run(){
PrintWriter outputStream=null;
试一试{
int x=0;
while(运行线程){
FileOutputStream输出=新的FileOutputStream(getRootDirectory()+文件名,true);
for(int i=17;i
这是两个运行线程的通用示例,您可以根据需要对其进行个性化设置,希望对您有所帮助

 class RunnableDemo implements Runnable {
   private Thread t;
   private String threadName;

 RunnableDemo( String name){
    threadName = name;
    System.out.println("Creating " +  threadName );
   }
    public void run() {
  System.out.println("Running " +  threadName );
  try {
     for(int i = 4; i > 0; i--) {
        System.out.println("Thread: " + threadName + ", " + i);
        // Let the thread sleep for a while.
        Thread.sleep(50);
     }
 } catch (InterruptedException e) {
     System.out.println("Thread " +  threadName + " interrupted.");
 }
 System.out.println("Thread " +  threadName + " exiting.");
}

public void start ()
{
  System.out.println("Starting " +  threadName );
  if (t == null)
  {
     t = new Thread (this, threadName);
     t.start ();
  }
}

}

public class TestThread {
public static void main(String args[]) {

  RunnableDemo R1 = new RunnableDemo( "Thread-1");
  R1.start();

  RunnableDemo R2 = new RunnableDemo( "Thread-2");
  R2.start();
}   
}

请发布a。如果您从多个线程写入同一个文件,可能会发生损坏,您应该使用文件锁,或滚动您自己的锁定系统以防止此问题。使用多个线程可以允许您a)减少网络延迟,或b)如果您有更多CPU,则使用更多CPU电源,然而,它不会1)让你的网络上传速度更快2)让你的磁盘旋转更快。当使用多个线程会产生任何影响时,您必须了解瓶颈是什么。大多数磁盘的写入速度比大多数internet连接快得多。即使使用加密和压缩,也很可能不受CPU的限制。如果你有超过1 Gbps的连接,你的网络几乎肯定是瓶颈,在这种情况下,只有增加你的网络带宽才有帮助。我必须使用连接,因为这是一种VoIP过程。非常感谢。为我工作!!!