2017-04-19 2 views
0

나는 BlockingQueue를 사용하여 프로듀서 소비자 시나리오를 구현 중이다.프로듀서 컨슈머 (consumer) 시나리오. 실행이 완료 할 때까지 큐로부터 읽는 것을 정지한다.

이 내 소비자 방법을 실행하는 것입니다 :

@Override 
    public void run() { 
     try { 
      Message msg; 
      System.out.println("Consumer started"); 
      //consuming messages until exit message is received 

      while(!(msg=queue.take()).getMsg().equalsIgnoreCase("exit")) { 

       // 




      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

을하고 내가 대기열에서 무언가를 얻을 때 아래의 메소드를 호출하려고하지만, 여기에 조건이다. 나는이 방법의 하나의 무한함을 실행하고 싶다. 아래 메서드에서 반환 얻을 때까지 대기열에서 소비하고 싶지 않아.

저는 while 루프에서 이것을 어떻게 포함할까요?

public boolean runStrategy(String msg) { 

     ExecutionContext executionContext = new ExecutionContext(); 

     String[] filePathArray = msg.split("/"); 

     String campaignForType = filePathArray[6];// .equals("Profiles")/events etc etc 

     if (campaignForType.equalsIgnoreCase("Profiles")) { 

      executionContext.setExecutionStrategy(new ProfileUploadExecutionStrategy()); 
      return executionContext.executeCampaign(filePathArray, msg); 

     } else if (campaignForType.equalsIgnoreCase("Events")) { 

      /* executionContext.setExecutionStrategy(new EventExecutionStrategy()); 
      executionContext.executeCampaign(filePathArray, msg.toString());*/ 

      return true; 
     } 

     return true; 
    } 

답변

1

아주 쉬운 해결책 : 하나의 소비자 스레드 만 시작하십시오.

실행중인 메소드의 인스턴스를 하나만 원할 경우. 아니면 다른 경우가 있습니까? 스레드가 하나만있는 경우 메서드의 인스턴스 하나만 실행할 수 있습니다. 해당 스레드가 사용 중이면 큐에서 소모 할 수있는 다른 스레드는 없습니다.

관련 문제