java - 使用 ListBlockingQueue 线程的并发运行由于某种原因被阻塞

标签 java multithreading concurrency

目前,我正在开发一个项目,我必须使用 ThreadsBlockingQueue 制作一个简单的程序:

该程序是凯撒编码,为什么 LinkedBlockingQueue 很好,因为它是一个赋值:)。

程序应从控制台读取字母(包括回车符和换行符 char(10) char(13)),这些字母将充当标志识别未编码字母的结束位置!

所以队列看起来像这样(假设移位因子为 3):

  • ['a'->'a'->'a'->'13'->'d'->'d'->'d']
  • 然后在编码时,正在读取的字母将被写入编码队列并从字母队列中删除。

所以我所做的如下:

代码:

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


public class Caesar {
   private final static int CR = 13,LF=10, SHIFT_FACTOR=3, OFFSET=23;

    public static void main(String[] args) throws InterruptedException {
        var br = new BufferedReader(new InputStreamReader(System.in));

        var letters = new LinkedBlockingQueue<Character>();

        var encoded = new LinkedBlockingQueue<Character>();
        var done = new LinkedBlockingQueue<Boolean>();

        Thread t1=new Thread(()-> takeInput(letters,br));
        Thread t2=new Thread(()->encode(letters,encoded));
        Thread t3=new Thread(()-> send(encoded,done));
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println(done.take());

    }

    private static void takeInput(BlockingQueue<Character>letters, BufferedReader br ) {
        System.out.println("Enter your input!.");
        int temp;
        try {
            while((temp=br.read())!=-1) {
                letters.put((char) temp);
                System.out.println("added "+ temp+ " and letters queue is "+letters);
                if (temp == CR || temp == LF){

                    break;
                }
            }
        }catch (IOException | InterruptedException e){ e.printStackTrace(); }
        finally {
            try {
                br.close();
            } catch (IOException e) { e.printStackTrace(); }
        }
    }

    public static void encode(BlockingQueue<Character>letters, BlockingQueue<Character>encoded) {

        try {
            char tempLetter;
            while(!letters.isEmpty()){//dont know why its not going in here!!!! BLOCKED HERE debugger dont want to get in here!
                System.out.println("inside encode");
                tempLetter=letters.take();
                if (tempLetter == CR ||tempLetter ==LF){
                    encoded.put(tempLetter);
                    break;
                }else if (tempLetter == ' ' || tempLetter == '.') {
                    encoded.put(tempLetter);
                }else if (cap(tempLetter) < 'X') {
                    encoded.put((char)(tempLetter+SHIFT_FACTOR));/
                }else{
                    encoded.put((char)(tempLetter - OFFSET));
                }
            }
        }catch (InterruptedException e) { e.printStackTrace(); }
    }

    private static Character cap(Character temp) {
        return temp >= 'a' ? Character.toUpperCase(temp):temp;/
    }

    private static void send( BlockingQueue<Character> encoded, BlockingQueue<Boolean> done){
        try {
            char temp=encoded.take();
            while (!encoded.isEmpty()){
                System.out.println("stuck in encode!");
                if(temp==CR || temp==LF)
                    break;
                System.out.print(temp);
                temp=encoded.take();
            }
            done.put(true);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

所需输出

Enter your input!.
aaaa
started encoding
the encoded elements are : -> dddd

我得到的输出:!

Enter your input!.
aaa
added 97 and letters queue is [a]
added 97 and letters queue is [a, a]
added 97 and letters queue is [a, a, a]
added 10 and letters queue is [a, a, a, 
]

(the program is on hold without and doesn't want to enter the while loop in the encrypt!!!!)

教授给了我们一个GO代码,作为实现功能的引用:

这里是 pastebin 上的 go 代码的链接

随意提出任何建议:)

最佳答案

    t1.start();
    t1.join();
    t2.start();
    t2.join();
    t3.start();
    t3.join();

您启动一个线程,然后等待它完成,然后再启动下一个线程。

将所有连接放在开始之后:

    t1.start();
    t2.start();
    t3.start();
    t1.join();
    t2.join();
    t3.join();

关于java - 使用 ListBlockingQueue 线程的并发运行由于某种原因被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62373946/

相关文章:

java - 如何获取对 Jersey 中调用的方法的引用

c - "Segmentation fault"在多线程中优先级为 linux 中的 c

Java多线程与方法调用结合使用?

java - Java 中的信号条件变量触发等待

java - 如何从 JDK 禁用 TLS 1.0

java - Java 反射 getMethod 有时不起作用的原因是什么?

java - 停止播放音频/广播并取消返回主屏幕的通知控制,音乐停止,通知停止,前台服务不起作用

c - 多线程 C 速度并不快

java - 如何从 Swing Gui 访问同步缓冲区?

multithreading - 什么是信号量?