用数据Java提供几个线程

时间:2014-12-25 14:21:53

标签: java multithreading queue

大家好,祝大家圣诞快乐。

我的部分代码存在问题。我的程序有一个线程,它从键盘输入并有几个等待输入的线程。

用户首先选择要发送该输入的线程。所以我们说我们有3个线程(0,1,2)加上获得键盘输入的线程。用户将首先选择他想要与之交互的线程,然后他将实际数据发送到该线程。

我有一段代码正在处理这个过程。我使用'LinkedBlockingQueue'来实现它。 键盘线程将数据放入队列中,“workers”(其他3个线程)从该队列中获取该数据。

问题是所有线程都在监听同一个队列,因此我在该队列中放入一个ID,让线程知道数据是指向它们还是指向其他线程。

以下是代码:

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();
                        }

正如您在代码中看到的,我所做的是检查处理信息的线程是否是指向的线程。如果是,我处理它,如果不是,我将数据再次放入排队让其他线程检查它。

如果我选择线程0与它进行交互工作。如果我选择其他人,它不会。

你能帮我解决这个问题吗???

非常感谢!

2 个答案:

答案 0 :(得分:1)

摆脱共享队列,让每个线程都有自己的队列。然后,当您获得输入时,只需将其分派到要接收它的相应线程的队列中。

答案 1 :(得分:1)

嗨,我做了一个小代码试试吧

 /*
  * 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();
  }

 }

这里我使用共享队列,但你必须尊重消息格式就像id1:hello或id2:lol