Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 22nd, 2006, 05:38 AM
Authorized User
Join Date: Dec 2005
Location: Kathmandu, Bagmati, Nepal.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sharing a data structure across multiple threads

Hi, I would like to share a data structure (e.g. HashMap) across multiple threads. Each thread will be adding an entry to the HashMap. A part of the code is:
HashMap info = new HashMap();
File[] contents = mainDir.listFiles();

for (int i = 0; i < contents.length; i++) {           
                    spawnThread st = new spawnThread(contents[i], info);
                                        Thread t = new Thread(st);

How can I get it done properly?

Reply With Quote
  #2 (permalink)  
Old October 9th, 2006, 08:32 PM
Friend of Wrox
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts

It looks like you've already passed the info HashMap to the new thread, so therefore the parent thread and the child thread should both have access to it. However, HashMaps are not thread-safe. Therefore you should have a synchronized() block around any code that does gets or sets on the HashMap.


    info.put("Foo", new Boolean(true));

    boolean foo = ((Boolean)info.get("Foo")).booleanValue();

Jon Emerson
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
data integration/sharing cpojoe BOOK: Beginning SharePoint 2007: Building Team Solutions with MOSS 2007 ISBN: 978-0-470-12449-9 4 November 9th, 2008 06:01 AM
Connecting to multiple com ports using threads gaurisharya Beginning VB 6 0 April 8th, 2008 12:01 AM
Accessing data from two threads lmadhavi Visual C++ 1 September 14th, 2004 06:59 AM
Record sharing by multiple users demivolt Access 1 August 26th, 2004 08:17 PM

All times are GMT -4. The time now is 05:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.