Peter Lawrey

20

votes
2

answer
1.6k

views

Why do some floating point numbers appear with a trailing 0

Does anyone know why the numbers 0.001 to 0.009 are rendered to a String with a trailing 0 but other numbers do not. e.g. numbers 0.01 to 0.09 do not. System.out.println(Locale.getDefault()); for (int i = 0; i
Peter Lawrey
20

votes
1

answer
393

views

RandomAccessFile.setLength much slower on Java 10 (Centos)

The following code public class Main { public static void main(String[] args) throws IOException { File tmp = File.createTempFile('deleteme', 'dat'); tmp.deleteOnExit(); RandomAccessFile raf = new RandomAccessFile(tmp, 'rw'); for (int t = 0; t < 10; t++) { long start = System.nanoTime(); int count =...
Peter Lawrey
21

votes
1

answer
484

views

Is there a reason Object.hashCode() is 31-bit?

If you run the following on HotSpot Java 7 64-bit version. int countTopBit = 0, countLowestBit = 0; for (int i = 0; i < 100000000; i++) { int h = new Object().hashCode(); if (h < 0) countTopBit++; if ((h & 1) == 1) countLowestBit++; } System.out.println('The count of negative hashCodes was ' + count...
Peter Lawrey
11

votes
1

answer
911

views

How to handle upper or lower case in JSR 310? [duplicate]

This question already has an answer here: How to parse case-insensitive strings with jsr310 DateTimeFormatter? 3 answers If a month is in UPPER or lower case, i.e. not Title case, DateTimeFormatter cannot parse the date. Is there a simple way to convert a date to title case, or a way to make the f...
Peter Lawrey
22

votes
4

answer
930

views

Testing a Java library using 100 - 1000 GB of memory

I have an open source library which is designed to support very large collections efficiently. I have tested it on my PC with 24 GB but would like to test it on much a larger system, up to 1 TB. Most hosting solutions don't support this sort of memory sizes and I only need access for brief periods...
Peter Lawrey
22

votes
1

answer
2.2k

views

How to reduce jitter for Java?

To resolve this issue I have created an open source Java Thread Affinity library When I have a number of thread interacting closely it can reduce latency and increase throughput. For single threaded tasks it can still reduce jitter quite a bit. This program looks at the difference in time between c...
Peter Lawrey
22

votes
4

answer
1.8k

views

Can you inspect the byte code of a Java 8 lambda at runtime?

If you have an anonymous class like Predicate isEmpty = new Predicate() { public boolean test(String t) { return t.isEmpty(); } }; A library which is passed the reference to isEmpty can inspect the byte code to see what it does and possibly manipulate it. Is there a way you can do this for lambdas?...
Peter Lawrey
12

votes
5

answer
5.7k

views

Will a long % int will always fit into an int

Can anyone confirm if this is true? Java will turn a long % int into a long value. However it can never be greater than the modulus to it is always safe to cast it to an int. long a = int b = int c = (int) (a % b); // cast is always safe. Similarly a long % short will always be safe to cast to a...
Peter Lawrey
31

votes
1

answer
644

views

In Java Lambda's why is getClass() called on a captured variable

If you look at the byte code for Consumer println = System.out::println; the byte code generates by Java 8 update 121 is GETSTATIC java/lang/System.out : Ljava/io/PrintStream; DUP INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class; POP INVOKEDYNAMIC accept(Ljava/io/PrintStream;)Ljava/util/fu...
Peter Lawrey
31

votes
2

answer
18.8k

views

What is the Metadata GC Threshold and how do I tune it?

In an application I have the following -verbose:gc [GC (Metadata GC Threshold) 8530310K->2065630K(31574016K), 0.3831399 secs] [Full GC (Metadata GC Threshold) 2065630K->2053217K(31574016K), 3.5927870 secs] [GC (Metadata GC Threshold) 8061486K->2076192K(31574016K), 0.0096316 secs] [Full GC (Metada...
Peter Lawrey
24

votes
3

answer
607

views

Is there a way to prevent ClosedByInterruptException?

In the following example, I have one file being used by two threads (in the real example I could have any number of threads) import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class A { static vol...
Peter Lawrey
35

votes
4

answer
9k

views

Incompatible type with Arrays.asList()

In the following example, if I have multiple types in the list it compiles ok, but if I have one element, it chooses a different type which is no longer assignable. // compiles fine List
Peter Lawrey
6

votes
1

answer
713

views

Can anyone think of a practical use for XMLGregorianCalendar.getEon()?

I came across the XMLGregorianCalendar class a while ago and I was impressed by the way it made GregorianCalendar look lightweight. ;) Recently, I noticed a method BitInteger getEon() which baffles me. From the Javadoc for XMLGregorianCalendar getYear() is a value between -(10^9-1) to (10^9)-1 or D...
Peter Lawrey
44

votes
6

answer
1.1k

views

Is there a way to speed up Javadoc (takes 7 minutes)

I am building a Javadoc for a module with 2,509 classes. This currently takes 7 min or 6 files per second. I have tried mvn -T 1C install However javadoc only uses 1 CPU. Is there a way to use more and/or speed up? I am using Oracle JDK 8 update 112. My dev machine has 16 cores and 128 GB of memory...
Peter Lawrey
45

votes
6

answer
3.3k

views

Ways to improve performance consistency

In the following example, one thread is sending 'messages' via a ByteBuffer which is the consumer is taking. The best performance is very good but its not consistent. public class Main { public static void main(String... args) throws IOException { for (int i = 0; i < 10; i++) doTest(); } public sta...
Peter Lawrey
58

votes
5

answer
11.9k

views

Support for Compressed Strings being Dropped in HotSpot JVM?

On this Oracle page Java HotSpot VM Options, it lists -XX:+UseCompressedStrings as being available and on by default. However in Java 6 update 29, it is off by default and in Java 7 update 2 it reports a warning Java HotSpot(TM) 64-Bit Server VM warning: ignoring option UseCompressedStrings; suppor...
Peter Lawrey
70

votes
6

answer
2.2k

views

Strange array return type

Has any one seen the array [] placed after the method signature like this? public static String mySplit(String s)[] { return s.split(','); } public static void main(String... args) { String[] words = mySplit('a,b,c,d,e'); System.out.println(Arrays.toString(words)); } prints [a, b, c, d, e] In the pa...
Peter Lawrey
69

votes
3

answer
8.4k

views

Is there a way to compare lambdas?

Say I have a List of object which were defined using lambda expressions (closures). Is there a way to inspect them so they can be compared? The code I am most interested in is List strategies = getStrategies(); Strategy a = (Strategy) this::a; if (strategies.contains(a)) { // ... The full code is i...
Peter Lawrey
2

votes
1

answer
829

views

Arrays as proper objects [closed]

I have written a page here on using arrays as proper objects with their own methods instead of relying on helper classes like Arrays, Arrays and ArrayUtils. ints.sort(); // instead of Arrays.sort(ints); // instead of int[] onemore = ArrayUtils.add(ints, 8); int[] onemore = ints.add(8); I am sure...
Peter Lawrey
3

votes
1

answer
378

views

How do I flush the buffered data in a DeflatorOutputStream

I would like to be able to flush any pending data in a DeflaterOutputStream and read it in a InflaterInputStream. However, it appears that flush(), doesn't do this. Is there another way? ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(b...
Peter Lawrey
120

votes
8

answer
5.4k

views

Any idea why I need to cast an integer literal to (int) here?

In the following example int i = -128; Integer i2 = (Integer) i; // compiles Integer i3 = (Integer) -128; /*** Doesn't compile ***/ Integer i4 = (Integer) (int) -128; // compiles Integer i4 = -128; // compiles Integer i5 = (int) -128; // compiles Integer i6 = (Integer) (-128); // compiles Integer i7...
Peter Lawrey
15

votes
3

answer
535

views

Java doesn't allow arrays of inner classes for a generic class

I know that you cannot create an array of a generic type, Instead you have to resort to a hack. (Given Java supports generic arrays, just not their creation, it is not clear to me why a hack is better than Java supporting creating generic arrays) Instead of writing this Map.Entry[] entries = new Ma...
Peter Lawrey
31

votes
3

answer
1.9k

views

Is there any reason EnumMap and EnumSet are not Navigable

Enum is Comparable which means you can have NavigableSet modes = new TreeSet(); NavigableMap modeMap = new TreeMap(); These have O(ln N) access times. The Enum collections have O(1) access times, but are not Navigable NavigableSet modes = EnumSet.noneOf(AccessMode.class); // doesn't compile Navigabl...
Peter Lawrey
27

votes
1

answer
1.4k

views

Memory barriers and the TLB

Memory barriers guarantee that the data cache will be consistent. However, does it guarantee that the TLB will be consistent? I am seeing a problem where the JVM (java 7 update 1) sometimes crashes with memory errors (SIGBUS, SIGSEG) when passing a MappedByteBuffer between threads. e.g. final Atomic...
Peter Lawrey
537

votes
5

answer
70.1k

views

Why does Math.round(0.49999999999999994) return 1?

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5. for (int i = 10; i >= 0; i--) { long l = Double.doubleToLongBits(i + 0.5); double x; do { x = Double.longBitsToDouble(l); System.out.println(x + ' rounded is ' + Math.round(x)); l--; } while (...
Peter Lawrey
22

votes
4

answer
2.1k

views

What is an “internal address” in Java?

In the Javadoc for Object.hashCode() it states As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation...
Peter Lawrey
21

votes
1

answer
3.8k

views

Why do -Xmx and Runtime.maxMemory not agree

When you add -Xmx????m to the command line, the JVM gives you a heap which is close to this value but can be out by up to 14%. The JVM can give you a figure much closer to what you want, but only through trial and error. System.out.println(Runtime.getRuntime().maxMemory()); prints -Xmx1000m -> 93...
Peter Lawrey