Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why only mark() and reset() method are synchronized in java.io.InputStream?

Don't understand why mark() and reset() are synchronized and why read() not?

like image 880
xin zhao Avatar asked Jun 11 '15 08:06

xin zhao


People also ask

What does it mean if a method is final synchronized?

The synchronized keyword causes a thread to obtain a lock when entering the method, so that only one thread can execute the method at the same time (for the given object instance, unless it is a static method).

When should a method be synchronized?

Synchronization is needed when Object is mutable. If shared Object is immutable or all the threads which share the same Object are only reading the Object's state not modifying then you don't need to synchronize it. Java programming language provide two synchronization idioms: Methods synchronization.

Why do we synchronize Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.

Can we synchronize method in Java?

Java Synchronized MethodSynchronized method is used to lock an object for any shared resource. When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.


1 Answers

java.io.InputStream is an abstract class. It has a default implementation for mark/reset that only throw an exception on reset telling that is not supported so subclasses that don't support it don't need to code their own method throwing the exception. "synchronized" is not useful for the default case, to throw an exception.

Any subclass that supports it will have to override those methods and synchronization is not inherithed so the overriden methods may or may not be synchronized.

I think it does not have any effect.

I guess it is a design flaw without consequences or maybe it is a warning so programmers that subclass it to synchronize those methods too because it should be made that way.

like image 119
aalku Avatar answered Sep 28 '22 00:09

aalku