"is the scanner in java not thread safe?" Code Answer

1

if you use the same instance of scanner in two threads you will have trouble unless you synchronise access to the object. but two separate instances of scanner will never interfere with each other.

edit in response to comment asking how to synchronise

first, are you really sure you need to synchronise at all? you can safely use different scanner instances in different threads without any danger. one thread can have
scanner s1 = new scanner (new file ("/tmp/file1.txt");
and another thread can have
scanner s2 - new scanner (new file ("/tmp/file2.txt"));
and there is no risk. the different scanners can use the same file, different files or completely different data sources. you still need to be cautious though. as noted by stephen c below you will still get broken operation if two separate instances of scanner are using the same stream or reader as their input then they will steal characters from each other. this warning applies to the constructors using inputstream, readable and readablebytechannel.

the problem with using a single scanner in multiple threads is that it is sequentially consuming characters from a single source. if you have multiple threads consuming these characters in unsynchronised fashion then each thread will get some of the characters and none of the threads will get all of the characters. to illustrate: imagine you have a scanner reading the string "qwertyuiop", and two separate threads each call function next() at the same time, then one thread might see "ertip" and the other thread would get "qwyuo"; which would be useless.

my suggestions to synchronise this are:

  1. do not multi-thread! even amputation of body parts is preferrable to trying to make a multi-threaded application stable, scalable and flexible!
  2. sometimes you can subclass a non-thread-safe class (or encapsulate and delegate) and synchronise calls to the base class (or delegate): synchronised (this) { super.next (); }. but don't try this with scanner! there are so many consumer methods, and you have no idea how the class is implemented internally so you are doomed to fail! see suggestion 1.
  3. what i would try to do here is have a single thread running the scanner and feeding the tokens into an arrayblockingqueue. that way you will get complete tokens going into the queue in their correct order. you can have as many threads as you like reading from the queue. but be aware that any of your threads can get blocked either reading or writing this queue unless you take care to handle full and empty conditions. chances are that no matter what you do you will end up with dangling threads that never finish. see point 1. this will get complicated if you want to sometimes call different next methods (e.g. nextint(), nextdouble()) or use the has methods (e.g. hasnextint(), hasnextdouble()), but not as complicated as point 2.

finally, i would suggest you see point 1.

By mxxk on June 20 2022

Answers related to “is the scanner in java not thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.