小渣飞、 2020-12-21 15:07 采纳率: 87.9%
浏览 71
已采纳

Java 中多线程同步布尔值的问题?

 

找了一天的 Bug 还是找不到是那个地方出了问题,目前线程2卡死,原因是布尔值不同步,可是我使用 static 关键字了,请问为什么在线程1设置为 true 后,线程2还是false,求解决思路

import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;


public class ThreadCopy {
    private static ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
    private static final Scanner scanner = new Scanner(System.in);
    public static String[] types;
    public static File workPath;
    public static File savePath;
    public static int copyErrorCount;
    public static int copySuccessCount;
    public static boolean isFlag = false;

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

        System.out.println("请输入一个工作路径:");
        workPath = new File(scanner.nextLine());
        System.out.println("请输入一个存储路径:");
        savePath = new File("/Users/jeffrey/test/test2");
        System.out.println("请输入筛选文件的类型,如有多个使用逗号隔开:");
        types = scanner.nextLine().replace(" ", "").split(",");


        if (workPath.exists() && savePath.exists()) {
            Thread t1 = new Thread(new ThreadForFindFile(queue, workPath, types));
            isFlag = true;

            Thread t2 = new Thread(new ThreadForCopyFile(queue, savePath));
            t1.start();
            t2.start();

            while (true) {

                if (!t1.isAlive()) {
                    isFlag = true;
                }
                
                if (!t1.isAlive() && !t2.isAlive()){
                    System.out.println("程序结束");
                    break;
                }
            }

        } else {
            throw new RuntimeException("请输入有效地工作路径或存储路径");
        }
    }
}

class ThreadForFindFile extends Thread {

    private final ConcurrentLinkedQueue<String> queue;
    private final File srcPath;
    private final String[] fileTypes;

    public ThreadForFindFile(ConcurrentLinkedQueue<String> queue, File srcPath, String[] fileTypes) {
        this.queue = queue;
        this.srcPath = srcPath;
        this.fileTypes = fileTypes;
    }

    @Override
    public void run() {
        find(this.srcPath);
    }

    private void find(File srcPath) {
        File[] files = srcPath.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    find(file);
                } else {
                    String[] split = file.toString().split(File.separator);
                    String[] suffix = split[split.length - 1].split("\\.");
                    if (Arrays.toString(fileTypes).contains(suffix[suffix.length - 1])) {
                        this.queue.add(file.toString());
                    }
                }
            }
        }
    }
}

class ThreadForCopyFile extends Thread {
    public static int copyErrorCount = ThreadCopy.copyErrorCount;
    public static int copySuccessCount = ThreadCopy.copySuccessCount;
    public static boolean ThreadOneIsAlive = ThreadCopy.isFlag;

    private final ConcurrentLinkedQueue<String> queue;
    private final File savePath;

    public ThreadForCopyFile(ConcurrentLinkedQueue<String> queue, File savePath) {
        this.queue = queue;
        this.savePath = savePath;
    }

    @Override
    public void run() {
        System.out.println(ThreadOneIsAlive);

        while (!ThreadOneIsAlive) {
            String file = queue.poll();
            try {
                if (file != null) {
                    String[] split = file.split(File.separator);
                    String name = split[split.length - 1];
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(savePath + File.separator + name));
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, len);
                    }
                    copySuccessCount++;
                }

            } catch (Exception e) {
                copyErrorCount++;
                e.printStackTrace();
                break;
            }

        }

    }
}


  • 写回答

3条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 C++实现更大的整数类
      • ¥15 Python怎么实现加减乘除乘方和阶乘呀
      • ¥15 centos8-arm64平台编译安装ray分布式计算框架。
      • ¥20 求帮做一个安卓app,用Android Studio制作。需要整个项目文件。
      • ¥15 使用深度学习对于楼梯尺寸进行检测
      • ¥15 特别是3和4 帮帮忙
      • ¥15 C++完成两个子类的构建,基类已给出
      • ¥66 求拉格朗日插值某点的值
      • ¥15 简易的机票预定信息系统
      • ¥15 C++实现对称图形小程序