Hash Tables and stuff

Hiya all,

I'm slightly confused over Hashtables. At the moment I have a program that reads a row of data from a hash table then sends it over a socket to another (hopefully) networkable component. However, if I wanted to thread the send program (that sends a line of data then removes it from the hashtable) would I run into concurrency problems with say two send threads trying to access the hastable at the same time? The only other option I can think of if this is the case is to include the hashtable methods within the send thread but then I'd be making multiple single lined hashtables. Any ideas?

On another topic entirely, currently the send method does a System.out.println to the consol to inform the user that data has been sent, socket is connected etc. Is there any way I could create an interface then send that consol data to a TextArea in the interface instead? I'm not that hot on all this swing voodoo as you might of guessed!!

Thanks for your time all!

[1002 byte] By [G@Trematona] at [2007-9-29 23:06:51]
# 1

In answer to your hashtable question, the Hashtable class is thread save. However if you were using HashMap it would be an issue because HashMap is not thread save.

And yes it is possible to send the output to a swing component, the problem I have had with that is keeping the buffer size down.

Some classes to have a look at for this are:

JFrame

JTextPane (add this to you JFrame)

Document (add this to your JTextPane)

you can now use the InsertString and remove methods to control what the user sees.

Using StringWriter (instead of PrintWriter as in System.out) you can write your logging to a string, and then update the Document with this string.

I hope this is of use to you.

Trawling through the javadocs with help you http://java.sun.com/j2se/1.4.2/docs/api/index.html

packages you are interested in are javax.swing and java.io

JElliota at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 2

Hello,

HASHMAP

First of all

I learned that a HashTable is getting old.

Maybe you can use a HashMap ?

It has +/- the same methods

( http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html )

THREADS

If you want to access your HashTable/Map you have to make an extra class.

Example

You have clients and producers and you will have 1 SOURCE.

The source class will contain your HASHMAP/TABLE !

the methods to access and to put some information in your map/table must be synchronized

First you have to create a source.

After you created that source, you will make the clients and the producers, the source object

will be a parameter of the constructor in your clients and producers.

Clients: are classes that use the information in your hashtable/map

producers: they create the information and put it in the table

(both are "extends Thread" )

an example of a method in your source

class attribute will control the access !!

private static int access = 0;

// 0 = you can access

// 1 = you can't access the map, because other classes are using the stuff

public class Source extends Thread

{...

public synchronized void setValue(int yourInt)

{

while( access == 1)

{

try

{

wait();

}

catch(InterruptedException ex)

{

ex.printStackTrace();

}

}

// say the class that you will use the map

access = 1;

// put the value in your hashMap

yourHashMap.put(new Integer(yourInt));

// let your thread free and say the sourceclass that others may use it

notify();

access = 0;

}

}

greetz

Kristof

Kristof_DCa at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 3

No no no.

That is already done for you with Hashtable but not with HashMap. If you want to use threads use Hashtable, if you are not using threads use HashMap. You do not have to do any synchronization for hashtable, but you would have to for HashMap if you were silly enough to use one in a multi-thread environment.

JElliota at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 4

> In answer to your hashtable question, the Hashtable

> class is thread save. However if you were using

> HashMap it would be an issue because HashMap is not

> thread save.

>

But it's easy to get a thread-safe HashMap if you really need one:

java.util.Map unsafeMap = new java.util.TreeMap();

java.util.Map safeMap = java.util.Collections.synchronizedMap(unsafeMap);

MOD

duffymoa at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 5

>

> But it's easy to get a thread-safe HashMap if you

> really need one:

>

> > java.util.Map unsafeMap = new java.util.TreeMap();

> java.util.Map safeMap =

> java.util.Collections.synchronizedMap(unsafeMap);

>

>

> MOD

Oops, might have been a better example if I'd used HashMap instead of TreeMap, but you get the idea. ;) - MOD

duffymoa at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 6
or how about:Map saveHashMap = new HashTable();HashMap provided identical functionality to HashTable, why bother?
JElliota at 2007-7-16 3:31:16 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...