2

I used the Rox Java NIO tutorial to adapt and create a multithread TCP server (http://rox-xmlrpc.sourceforge.net/niotut/).

But now I'm lost, because I need to find a way to share a file and an in-memory arraylist across all threads. Inside each thread I need to be able to write to that global file and array, read from it to insert into a table and to remove processed data from the file and from the array.

Is this possible? How can I do it? How can I garantee synchronization? Could you point me to a tutorial or an example?

Best Regards, and happy new year!

ThelmaJay
  • 79
  • 1
  • 14
  • The ROX NIO tutorial is garbage. Guy doesn't even know that closing the channel cancels the key, and he invents a lot of non-existent difficulties about other things as well. – user207421 Sep 04 '19 at 13:25

1 Answers1

0

Java collections are by default not synchronized. There is a big notice in the ArrayList documentation (the bold text in the class description) to that effect. Fortunately for you, there is also a method Collections.synchronizedList() that returns a wrapper for your ArrayList, or any other List that will be synchronized properly so you do not have to do it yourself.

As far as accessing the shared wrapper object, you can either pass a reference to your thread or have a class with static or singleton (global) access to the objects you are sharing. The second option is probably better if you have may shared objects.

If you want to read and write to a file, you can use the RandomAccessFile class. Here, synchronization will be trickier because the class is not thread safe. You may want to put synchronization into your static/singleton class that has the array list. Here is a thread on synchronizing file access: Java: thread-safe RandomAccessFile.

For your purposes, it may be easier to use a logger. This is an object that is inherently synchronized. A basic logging API is provided by the java.util.logging package. Here is a good tutorial on logging: http://www.vogella.com/articles/Logging/article.html

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Thank you @Mad_Physicist. So what should I do? Create a global arraylist with that wrapper and then accessed it inside each thread? Should I pass it as an argument to the run method? Can I do the same with a file? – ThelmaJay Jan 02 '14 at 17:42
  • @ThelmaJay. You are not being very clear about the file part. Do you want to have a `File` object and each thread opens/closes it? Do you want to have a RW file always open and accessible to all threads? – Mad Physicist Jan 02 '14 at 17:48
  • Sorry. A RW file always open and accessible to all threads. Because in each thread if the insertion into the database fails I want to write the data into the file. But I also want to read it from inside the thread so i can try to insert it again into the db. – ThelmaJay Jan 02 '14 at 17:52
  • Are you looking for a logger? – Mad Physicist Jan 02 '14 at 17:55
  • It's more a fail system. I have hundreds of devices connecting to the server. Each device is going to be served by a thread. Each device sends a stream of data that I insert into a db table so it can be processed by other module (the parser). The problem is, once the stream is sent I no longer can receive it again. So if there is a problem with the db, I lose that data forever. So I would like to be able to store it in memory or in a file. – ThelmaJay Jan 02 '14 at 18:01
  • 1
    That sounds like an awful lot of backup. Why not just log the failures? Then you can write a small script to parse your log file to get the failed entries and decide how to process them. – Mad Physicist Jan 02 '14 at 18:02
  • You are right. How can I do that? Could you point me to some resource/tutorial? – ThelmaJay Jan 03 '14 at 12:00