简体   繁体   English

用数据Java喂几个线程

[英]Feeding several threads with data Java

Hello and happy Christmas to everyone. 大家好,圣诞节快乐。

I have a problem with a part of my code. 我的部分代码有问题。 My program have a thread that is getting input from the keyboard and have several threads that are waiting for that input. 我的程序有一个正在从键盘获取输入的线程,并且有几个正在等待该输入的线程。

The users selects first to what thread he is going to send that input. 用户首先选择要发送该输入的线程。 So lets says that we have 3 threads (0,1,2) plus the thread that gets the keyboard input. 因此,让我们说我们有3个线程(0,1,2)加上获取键盘输入的线程。 The user will select first what thread he wants to interact with and after that he will send the actual data to that thread. 用户将首先选择要与之交互的线程,然后将实际数据发送到该线程。

I have a piece of code that is taking care of that process. 我有一段代码正在处理该过程。 I use ´LinkedBlockingQueue´ to achieve it. 我使用“ LinkedBlockingQueue”来实现。 The keyboard thread puts data in the Queue and the "workers" (the other 3 threads) get that data from that queue. 键盘线程将数据放入队列中,“工作人员”(其他3个线程)从该队列中获取该数据。

The problem is that all the threads are listening for that same Queue so I put an ID in that Queue to let the threads know if the data is directed to them or to other thread. 问题是所有线程都在侦听同一队列,因此我在该队列中放置了一个ID,以使线程知道数据是定向到它们还是其他线程。

Here is the code: 这是代码:

Thread Thread_OUT = new Thread(new Runnable() {

                @SuppressWarnings("unchecked")
                @Override
                public void run() {
                     while(true) {
                    try {
                        Object recibido= sharedQueue.take();
                        sharedQueue.put(recibido);          
                        //System.out.println("Im the thread "+ clientID+" and I got "+recibido.toString());
                        if(Integer.parseInt(recibido.toString())==clientID){ // If it is for me I get the data
                            String x = CommandShellServer.data.get(clientID); // just get the data (it is in a hashmap)
                            CommandShellServer.data.clear(); // empty the hashmap
                            sharedQueue.clear();

                            OUT = do_something(x);

                        }
                        else{ // If it is not I will forward it to other thread
                            Thread.currentThread().wait(100);
//                          sharedQueue.put(recibido);
//                          sharedQueue.clear();
                        }

As you can see in the code what I do is checking if the thread that is handling the information is the one that is directed to If it is, I process it and if it is no I put that the data again in the queue to let the other threads to check for it. 正如您在代码中看到的那样,我正在检查正在处理信息的线程是否是定向到的线程。如果是,则对它进行处理,如果不是,则将数据再次放入队列中,以便其他线程进行检查。

If i select the thread 0 to interact with it works. 如果我选择线程0与之交互。 If I select others it doesnt. 如果我选择其他人,则不会。

Can you help me to try to solve this situation??? 您能帮我解决这种情况吗???

Thank you very much! 非常感谢你!

Get rid of the shared queue, and let each thread have its own. 摆脱共享队列,让每个线程都有自己的线程。 Then, when you get an input, just dispatch it to the queue of appropriate thread that is intended to receive it. 然后,当您获得输入时,只需将其分派到旨在接收它的适当线程的队列中即可。

hi that a little code i made try it 嗨,我做了一些代码尝试

 /*
  * To change this license header, choose License Headers in Project Properties.
  * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */
 package Application;

 import java.util.ArrayList;
 import java.util.Scanner;

 /**
 *
  * @author husseyn
 */
 public class producteurConsomateur {

 static Scanner clavier;
 static ArrayList<String> queu;

 public static void main(String[] args) {
    queu=new ArrayList<>();
    new Thread(){

        @Override
        public void run() {
            clavier=new Scanner(System.in);
            while (true) {                    
                try {
                    sleep(1000);
                } catch (Exception e) {
                }
                System.out.print("tape message :");
                String nextLine = clavier.nextLine();

                queu.add(nextLine);
               // notifyAll();



            }
        }

    }.start();


    new  Thread(){

        @Override
        public void run() {

            while (true) {  
                try {

                try {
                    wait();
                } catch (Exception e) {
                }
                synchronized(this){
                String get = queu.get(0);
                String[] messageFormat = get.split(":");
                String id=messageFormat[0];
                if (id.toLowerCase().equals("id1")) {
                      String message=messageFormat[0];
                      queu.remove(0);
                      System.out.println("message recived to thread ID1 :"+message);
                }}
                } catch (Exception e) {
                }

            }
        }

    }.start();

    new  Thread(){

        @Override
        public void run() {

            while (true) {  
                try {

                try {
                    wait();
                } catch (Exception e) {
                }
                synchronized(this){
                String get = queu.get(0);
                String[] messageFormat = get.split(":");
                String id=messageFormat[0];
                if (id.toLowerCase().equals("id3")) {
                      String message=messageFormat[0];
                      queu.remove(0);
                      System.out.println("message recived to thread ID3 :"+message);
                }}
                } catch (Exception e) {
                }

            }
        }

    }.start();

    new  Thread(){

        @Override
        public void run() {

            while (true) {  
                try {

                try {
                    wait();
                } catch (Exception e) {
                }
                synchronized(this){
                String get = queu.get(0);
                String[] messageFormat = get.split(":");
                String id=messageFormat[0];
                if (id.toLowerCase().equals("id2")) {
                      String message=messageFormat[0];
                      queu.remove(0);
                      System.out.println("message recived to thread ID2 :"+message);
                }}
                } catch (Exception e) {
                }

            }
        }

    }.start();
  }

 }

and here i use a shared queue but you have to respect the message format is like id1:hello or id2:lol 在这里我使用共享队列,但是您必须尊重消息格式,例如id1:hello或id2:lol

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

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