BitSet
. Add the implementation of the setBit()
method.BitSet
size can be considered constant. public class ConcurrentBitSet { private final AtomicLongArray bits; public ConcurrentBitSet(int size) { assert size >= 0; int words = (size + 63) / 64; bits = new AtomicLongArray(words); } public void setBit(int index) { // TODO: Implement me! } }
updateAndGet()
/ getAndUpdate()
, available with Java 8, might look like this: public void setBit(int index) { int word = index >> 6; long mask = 1L << index; bits.updateAndGet(word, value -> value | mask); }
compareAndSet()
looks like: public void setBit(int index) { int word = index >> 6; long mask = 1L << index; long oldValue; long newValue; do { oldValue = bits.get(word); newValue = oldValue | mask; } while (!bits.compareAndSet(word, oldValue, newValue)); }
enum
. What did she not consider? boolean sameEnum(Object o1, Object o2) { return o1.getClass().isEnum() && o1.getClass() == o2.getClass(); }
Enum.compareTo():
public final Class<E> getDeclaringClass() { Class<?> clazz = getClass(); Class<?> zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper; }
boolean sameEnum(Object o1, Object o2) { return o1 instanceof Enum && o2 instanceof Enum && ((Enum) o1).getDeclaringClass() == ((Enum) o2).getDeclaringClass(); }
interface Link<T> { T next(); }
getTail()
) method so that the code compiles without errors and warnings. Link getTail(Link head) { if (head.next() == null) { return head; } return getTail(head.next()); }
<T extends Link<T>> Link<T> getTail(Link<T> head) <T extends Link<T>> Link<T> getTail(T head) <T extends Link<T>> T getTail(T head)
<T extends Link<T>> T getTail(Link<T> head)
void send(SocketChannel ch, String message) throws IOException { byte[] bytes = message.getBytes(); ByteBuffer header = ByteBuffer.allocate(4); header.putInt(bytes.length); ch.write(header); ch.write(ByteBuffer.wrap(bytes)); }
ByteBuffer
you must call the flip () / rewind () / position (0) method, otherwise nothing will be written to the SocketChannel
(silently!) void send(SocketChannel ch, String message) throws IOException { byte[] bytes = message.getBytes(StandardCharsets.UTF_8); ByteBuffer header = ByteBuffer.allocate(4); header.putInt(bytes.length); header.flip(); while (header.hasRemaining()) { ch.write(header); } ByteBuffer body = ByteBuffer.wrap(bytes); while (body.hasRemaining()) { ch.write(body); } }
-Xmx
-XX:MaxMetaspaceSize
-XX:ReservedCodeCacheSize
-XX:+UseContainerSupport
-XX:MaxRAMPercentage
UseContainerSupport
parameter together with MaxRAMPercentage
affects only the size of the heap. Thus, there is no guaranteed way to avoid exceeding the limit using only the JVM flags, and the correct answer is the last one. You can find out more about Java memory usage by the process in Andrei Pangin's report on Joker 2018 “Java memory by process” .Source: https://habr.com/ru/post/447218/
All Articles