2017-11-29 1 views
-1

나는 파일에서 각 단어의 발생 횟수를 계산하고지도에 입력하는 자바 프로그램을 만들고있다. 스레드 개념을 사용하고 있습니다.왜지도 내의 단어와 일치하지 않는 스레드입니까?

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.LinkedHashMap; 
import java.util.Map; 



class MyThread1 extends Thread 
{ 
    String word; 
    int len,position; 
    String[] wordlist; 
    MyThread1(String word, int len,int position, String[] wordlist) 
    { 
     this.word=word; 
     this.len=len; 
     this.position=position; 
     this.wordlist=wordlist; 
    } 
public void run() 
{ 
    int i=position,y,count=0; 

     synchronized(this){ 
      Map<String,Integer> m=new LinkedHashMap<String,Integer>(); 

      if(m.containsKey(word)) 
     { 
      System.out.println("Duplicate entry - bypassing it"); 
     } 
     else{ 
     y=i+1; 
     count=1; 
     for(int j=y;j<len;j++) 
     { 
      if(word.equals(wordlist[j])) 
       count++; 
     } 
     /* if(m.containsKey(word)) 
     { 
      int l=m.get(word); 
      System.out.println("word is "+word); 
      System.out.println("existing count "+l); 
      System.out.println("current count "+count); 
      if(l>count) 
      { 
       m.put(word, l); 
      } 
      else 
      { 
       m.put(word,count); 
      } 
     } */ 
     m.put(word,count); 
      count=0; 
     } 

       for(Map.Entry<String,Integer> me:m.entrySet()) 
       { 
        System.out.println(me.getKey()+" "+me.getValue()); 
       } 
     } 
    } 
} 


public class UsingThread 
{ 
public static void main (String args[]) throws IOException 
{ 
    File f1=new File("C:/Users/sahithim/Desktop/file1.txt"); 
    FileReader fr=null; 
    int x; 
    String s="",eachEntry=""; 
    try { 
     fr=new FileReader(f1); 
     while((x=fr.read())!=-1) 
     { 
      s=s+(char)x; 
     } 
    } 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     fr.close(); 
    } 

    String[] wordArray=s.split(" "); 
     int num_0f_words=wordArray.length; 
     for(int i=0;i<num_0f_words;i++) 
     { 
      eachEntry=wordArray[i]; 
      MyThread1 mt1=new MyThread1(eachEntry,num_0f_words,i,wordArray); 
      mt1.start(); 
     } 



} 
    } 

이 글에서는 각 단어를 스레드로 보내고지도에 존재 여부를 확인합니다. 단어가지도에 표시 되더라도 인식하지 못합니다.

나는 내가 뭐하는 거지 실수는 무엇이

hello 6 
this 6 
hey 24 
    1 
is 6 
pallavi 6 
nalam 6 
hey 23 
hey 22 
hey 21 
sahithi 
    6 
i 12 
am 6 
good 6 
i 11 
love 6 
flowers 
    6 

    6 
hey 20 
hello 5 
this 5 
is 5 
pallavi 5 
nalam 5 
hey 19 
hey 18 
hey 17 
sahithi 
    5 
am 5 
i 10 
good 5 
i 9 
love 5 

    5 
flowers 
    5 
hey 16 


    1 
flowers 
    1 
is 4 
i 1 
this 4 
good 1 

    1 
am 1 
love 1 
sahithi 
    1 
i 2 
hey 2 
hey 1 
i 4 
i 3 
nalam 1 
this 1 
is 1 
hey 4 
hello 1 
love 2 

    2 
am 2 
flowers 
    2 
good 2 
hey 5 
sahithi 
    2 
hey 7 
hey 6 
nalam 2 
pallavi 2 
is 2 
this 2 
hey 8 
love 3 
i 5 

    3 
am 3 
sahithi 
    3 
hello 2 
nalam 3 
hey 11 
pallavi 3 
this 3 
hello 3 
hey 10 
hey 9 
i 6 
good 3 
flowers 
    3 
pallavi 1 
hey 3 
hello 4 
pallavi 4 
nalam 4 
hey 15 
hey 14 
sahithi 
    4 
hey 13 
i 8 
am 4 
good 4 

    4 
i 7 
love 4 
hey 12 
flowers 
    4 
is 3 

같은 몇 가지를 인쇄이

hey 24 
hello 6 
this 6 
is 6 
pallavi 6 
nalam 6 
sahithi 
    6 
i 12 
am 6 
good 6 
love 6 
flowers 6 

같은 출력을 기대하고있다? 필요한 출력을 얻는 방법?

+0

이것은 질문이 아니지만 다른 사람들이 코드를 작성하도록 요청한 것입니다. 한 단어 당 단 하나의 스레드 대신 단어의 각 발생에 대해 하나의 스레드를 시작합니다. – tkruse

+0

@tkruse 나는 다른 사람들에게 내 작업을 할 의도가 없다. 코드를 수정하고 필요한 출력을 얻었습니다. – sahithi

답변

0

단어 당 단 하나의 스레드가 아닌 각 단어의 발생마다 하나의 스레드를 시작합니다.

0

이 글에서는 스레드에 각 단어를 보내고지도에 존재 여부를 확인하고 있습니다. 단어가지도에 표시 되더라도 인식하지 못합니다.

당신은 단어 각각의 모든에 대한 새로운LinkedHashMap를 생성하는 새로운 스레드에 단어를 보내고있다. 나는 당신이 main 방법으로 할당하는 하나의 LinkedHashMap을 사용하고 싶다고 생각합니다. 당신이 단어 각각의 모든에 대한 새 스레드를 생성하는 경우

Map<String,Integer> wordMap = new LinkedHashMap<String,Integer>(); 
... 
MyThread1 myThread = new MyThread1(eachEntry, num_0f_words, i , wordArray, wordMap); 
// then use this external map inside of the `run()` method 

는하지만이 정말 Thread의 좋은 사용처럼 보이지 않는다. 하지만 이것은 아마도 일종의 학문적 운동일까요?