简体   繁体   English

线程同步 - 打印到文件夹

[英]Thread Synchronization - Printing to a FIle

I have two threads in my program, a "Sender" and "Receiver" thread. 我的程序中有两个线程,一个“Sender”和“Receiver”线程。 In both threads at various times, I need to write to a file. 在不同时间的两个线程中,我需要写入一个文件。 However, I am finding that sometimes they are both writing to the file at the same time causing the input to become jumbled. 但是,我发现有时他们都在同时写入文件导致输入变得混乱。

Is there a way for the program to only have one thread write to the file at a given time? 有没有办法让程序在给定时间只有一个线程写入文件?

In another thread: Thread Synchronization , I was told to use a Synchronization block. 在另一个线程: 线程同步 ,我被告知使用同步块。 However, when I try implement this I get errors as to how I am defining: 但是,当我尝试实现这个时,我得到的错误是我如何定义:

private final Object lock = new Object();

It initially says: 它最初说:

Cannot make a static reference to the non-static field lock 无法对非静态字段锁进行静态引用

If I change it to static, it then says 如果我将它改为静态,那就说

The method sychronized(Object) is undefined for the type SendThread . 对于SendThread类型,未定义SendThread sychronized(Object)方法。

Does synchronized(lock) NEED to be inside a function or can it just be around some code? synchronized(lock)是否需要在函数内部,还是只能在某些代码周围?

If this method would help my current issue, where and how should I define the above? 如果这种方法有助于我当前的问题,我应该在哪里以及如何定义上述内容?

The structure of my code is as follows: 我的代码结构如下:

public class my_main_class{

    private final static Object lock = new Object();

    public static void main (String[] args) throws Exception{

        class SendThread implements Runnable {

            synchronized (lock){
                  // contains code to print to text file
            }
         }

         class ReceiveThread implements Runnable {

            synchronized (lock){
                 // contains code to print to text file
            }
         }

}

synchronized marks a block of code as being executable only while holding a specific lock. synchronized将一段代码标记为仅在持有特定锁时可执行。 And in Java, you can't insert code anywhere, it has to be inside a method or an initialisation block. 在Java中,您无法在任何地方插入代码,它必须位于方法或初始化块中。

So your problem is not a multi-threading one but a basic Java syntax issue. 所以你的问题不是多线程问题,而是基本的Java语法问题。

I suppose this could do what you expected: 我想这可以达到你的预期:

class SendThread implements Runnable {
    public void run() {
        synchronized (lock){
              // contains code to print to text file
        }
    }
 }

 class ReceiveThread implements Runnable {
    public void run() {
        synchronized (lock){
              // contains code to print to text file
        }
    }
 }

Then in your main: 然后在你的主要:

private final static Object lock = new Object();

public static void main (String[] args) throws Exception{
    Runnable send = new SendThread();
    Runnable recv = new ReceiveThread();
    new Thread(send).start();
    new Thread(recv).start();
}

I have no idea what those methods are supposed to do so it might not be the right design, but at least it compiles and the two run methods are mutually exclusive. 我不知道这些方法应该做什么,所以它可能不是正确的设计,但至少它编译并且两个run方法是互斥的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM