File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Giving Unwanted Result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Giving Unwanted Result" Watch "Thread Giving Unwanted Result" New topic
Author

Thread Giving Unwanted Result

AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
Greetings to all,

I am facing a strange problem here. I have a HashMap having <String,Properties> signature. It contains 3 pairs of key,value set, namely, (value, prop), (really, prop), (belly,prop). It was initialised in a static {} bock by reading 3 separate files having 50 key1=value1, key2=value2 ..... key50=value50 and similarly for other files key1=really1 .... key50=really50 and key1=belly1 ... key50=belly50.
HashMap is a static variable, so only one instance.

Now, in main I create 3 threads and read all values and print them via thread. I am passing value / really / belly as parameter in threads, but it prints the last initialised belly all the time, instead of value or really.

Whats wrong I am doing here ? Any light. ? Also, attaching supporting files if anybody wants to run.


AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
Output That I am getting. All belly ....

name value
Cabinet = value key = key1 value = belly1
name really
Cabinet = really key = key1 value = belly1
name belly
Cabinet = belly key = key1 value = belly1
name value
Cabinet = value key = key2 value = belly2
name really
Cabinet = really key = key2 value = belly2
name belly
Cabinet = belly key = key2 value = belly2
name value
Cabinet = value key = key3 value = belly3
name really
Cabinet = really key = key3 value = belly3
name belly
Cabinet = belly key = key3 value = belly3
name value
Cabinet = value key = key4 value = belly4
name really
Cabinet = really key = key4 value = belly4
name belly
Cabinet = belly key = key4 value = belly4
name value
name really
Cabinet = really key = key5 value = belly5
Cabinet = value key = key5 value = belly5
name belly
Cabinet = belly key = key5 value = belly5
name value
Cabinet = value key = key6 value = belly6
name really
Cabinet = really key = key6 value = belly6
name belly
Cabinet = belly key = key6 value = belly6
name value
Cabinet = value key = key7 value = belly7
name really
Cabinet = really key = key7 value = belly7
name belly
Cabinet = belly key = key7 value = belly7
name really
Cabinet = really key = key8 value = belly8
name value
Cabinet = value key = key8 value = belly8
name belly
Cabinet = belly key = key8 value = belly8
name value
Cabinet = value key = key9 value = belly9
name really
Cabinet = really key = key9 value = belly9
name belly
Cabinet = belly key = key9 value = belly9
name really
Cabinet = really key = key10 value = belly10
name value
Cabinet = value key = key10 value = belly10
name belly
Cabinet = belly key = key10 value = belly10
name really
Cabinet = really key = key11 value = belly11
name value
Cabinet = value key = key11 value = belly11
name belly
Cabinet = belly key = key11 value = belly11
name really
Cabinet = really key = key12 value = belly12
name value
Cabinet = value key = key12 value = belly12
name belly
Cabinet = belly key = key12 value = belly12
name really
Cabinet = really key = key13 value = belly13
name value
Cabinet = value key = key13 value = belly13
name belly
Cabinet = belly key = key13 value = belly13
name really
Cabinet = really key = key14 value = belly14
name value
Cabinet = value key = key14 value = belly14
name belly
Cabinet = belly key = key14 value = belly14
name really
Cabinet = really key = key15 value = belly15
name value
Cabinet = value key = key15 value = belly15
name belly
Cabinet = belly key = key15 value = belly15
name really
Cabinet = really key = key16 value = belly16
name value
Cabinet = value key = key16 value = belly16
name belly
Cabinet = belly key = key16 value = belly16
name really
Cabinet = really key = key17 value = belly17
name belly
Cabinet = belly key = key17 value = belly17
name value
Cabinet = value key = key17 value = belly17
name really
name belly
Cabinet = really key = key18 value = belly18
Cabinet = belly key = key18 value = belly18
name value
Cabinet = value key = key18 value = belly18
name really
Cabinet = really key = key19 value = belly19
name belly
Cabinet = belly key = key19 value = belly19
name value
Cabinet = value key = key19 value = belly19
name really
Cabinet = really key = key20 value = belly20
name belly
Cabinet = belly key = key20 value = belly20
name value
Cabinet = value key = key20 value = belly20
name really
Cabinet = really key = key21 value = belly21
name belly
Cabinet = belly key = key21 value = belly21
name value
Cabinet = value key = key21 value = belly21
name really
Cabinet = really key = key22 value = belly22
name belly
Cabinet = belly key = key22 value = belly22
name value
Cabinet = value key = key22 value = belly22
name really
name belly
Cabinet = belly key = key23 value = belly23
Cabinet = really key = key23 value = belly23
name value
Cabinet = value key = key23 value = belly23
name really
Cabinet = really key = key24 value = belly24
name belly
Cabinet = belly key = key24 value = belly24
name value
Cabinet = value key = key24 value = belly24
name belly
Cabinet = belly key = key25 value = belly25
name really
Cabinet = really key = key25 value = belly25
name value
Cabinet = value key = key25 value = belly25
name belly
Cabinet = belly key = key26 value = belly26
name really
Cabinet = really key = key26 value = belly26
name value
Cabinet = value key = key26 value = belly26
name really
Cabinet = really key = key27 value = belly27
name belly
Cabinet = belly key = key27 value = belly27
name value
Cabinet = value key = key27 value = belly27
name belly
Cabinet = belly key = key28 value = belly28
name really
Cabinet = really key = key28 value = belly28
name value
Cabinet = value key = key28 value = belly28
name belly
Cabinet = belly key = key29 value = belly29
name value
Cabinet = value key = key29 value = belly29
name really
Cabinet = really key = key29 value = belly29
name belly
Cabinet = belly key = key30 value = belly30
name really
Cabinet = really key = key30 value = belly30
name value
Cabinet = value key = key30 value = belly30
name belly
Cabinet = belly key = key31 value = belly31
name really
Cabinet = really key = key31 value = belly31
name value
Cabinet = value key = key31 value = belly31
name belly
Cabinet = belly key = key32 value = belly32
name value
Cabinet = value key = key32 value = belly32
name really
Cabinet = really key = key32 value = belly32
name belly
Cabinet = belly key = key33 value = belly33
name value
Cabinet = value key = key33 value = belly33
name really
Cabinet = really key = key33 value = belly33
name belly
Cabinet = belly key = key34 value = belly34
name value
Cabinet = value key = key34 value = belly34
name really
Cabinet = really key = key34 value = belly34
name belly
Cabinet = belly key = key35 value = belly35
name value
Cabinet = value key = key35 value = belly35
name really
Cabinet = really key = key35 value = belly35
name belly
Cabinet = belly key = key36 value = belly36
name value
Cabinet = value key = key36 value = belly36
name really
Cabinet = really key = key36 value = belly36
name belly
Cabinet = belly key = key37 value = belly37
name value
Cabinet = value key = key37 value = belly37
name really
Cabinet = really key = key37 value = belly37
name belly
Cabinet = belly key = key38 value = belly38
name value
Cabinet = value key = key38 value = belly38
name really
Cabinet = really key = key38 value = belly38
name belly
Cabinet = belly key = key39 value = belly39
name value
Cabinet = value key = key39 value = belly39
name really
Cabinet = really key = key39 value = belly39
name belly
Cabinet = belly key = key40 value = belly40
name value
Cabinet = value key = key40 value = belly40
name really
Cabinet = really key = key40 value = belly40
name belly
Cabinet = belly key = key41 value = belly41
name value
Cabinet = value key = key41 value = belly41
name really
Cabinet = really key = key41 value = belly41
name belly
Cabinet = belly key = key42 value = belly42
name value
Cabinet = value key = key42 value = belly42
name really
Cabinet = really key = key42 value = belly42
name belly
Cabinet = belly key = key43 value = belly43
name value
Cabinet = value key = key43 value = belly43
name really
Cabinet = really key = key43 value = belly43
name belly
Cabinet = belly key = key44 value = belly44
name value
Cabinet = value key = key44 value = belly44
name really
Cabinet = really key = key44 value = belly44
name belly
Cabinet = belly key = key45 value = belly45
name value
Cabinet = value key = key45 value = belly45
name really
Cabinet = really key = key45 value = belly45
name belly
Cabinet = belly key = key46 value = belly46
name value
Cabinet = value key = key46 value = belly46
name really
Cabinet = really key = key46 value = belly46
name belly
Cabinet = belly key = key47 value = belly47
name value
Cabinet = value key = key47 value = belly47
name really
Cabinet = really key = key47 value = belly47
name belly
Cabinet = belly key = key48 value = belly48
name value
Cabinet = value key = key48 value = belly48
name really
Cabinet = really key = key48 value = belly48
name belly
name value
Cabinet = value key = key49 value = belly49
Cabinet = belly key = key49 value = belly49
name really
Cabinet = really key = key49 value = belly49
name value
Cabinet = value key = key50 value = belly50
name belly
Cabinet = belly key = key50 value = belly50
name really
Cabinet = really key = key50 value = belly50
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

AnirbanDelhi Pal wrote:

You only have one single Properties object that is shared for all keys of the map. The load method overwrites values so that's why you get "belly" - that's the last value read.
Move the declaration and creating of the Properties object inside the for loop, and each file will get its own Properties object with its own contents.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thomas Punihaole
Greenhorn

Joined: Jan 12, 2012
Posts: 4
Hashmap keys must be unique. If all your files use keys of the format "keyx" then when you load the first file the hashmap is populated. The second file overwrites the entries you just created. Finally, when you add belly it overwrites that and remains. So your hashmap only contains the last 50 entries:

key1->belly1
key2->belly2
...
key50->belly50
AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
Thank you Rob Spoor . It helped me a lot.
AnirbanDelhi Pal
Greenhorn

Joined: Jan 12, 2012
Posts: 6
@ Thomas Punihaole

I am not reading files directly in HashMap instead in Properties and then putting them in HashMap
thank you for your effort and patience to solve the issue.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Giving Unwanted Result