为什么线程不匹配地图中的单词?

时间:2017-11-29 04:47:17

标签: java multithreading

我正在做一个java程序,用于计算文件中每个单词的出现次数,并在地图中输入它们。我正在使用线程概念。

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



}  
    }

在此我将每个单词发送到一个帖子并检查它在地图中的存在。即使地图中存在该词,也无法识别它。

我期待像这样的输出

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

打印这样的东西

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

我在做什么错?如何获得所需的输出?

2 个答案:

答案 0 :(得分:0)

你为一个单词的每个出现开始一个线程,而不是每个单词只有一个主题。

答案 1 :(得分:0)

  

在此我将每个单词发送到一个帖子并检查它在地图中的存在。即使地图中存在该词,也无法识别它。

您正在向主题发送一个单词,该主题为每个单词创建 new LinkedHashMap。我相信您希望使用在LinkedHashMap方法中分配的单个main

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看起来并不合适。但也许这是某种学术活动?