1 public final class Singleton {
2 private static Singleton inst = null;
3 private static int property;
4
5 public Singleton() {}
6
7 public static getInstance() {
8 if (inst == null) {
9 inst = new Singleton();
10 }
11
12 return inst;
13 }
14
15 public static getProperty() { return property; }
16 public static setProperty(int val) { property = val; }
17 }
1 public static class ObjectPool {
2 private static final Queue<Object> inuse = new Queue<Object>();
3 private static final Queue<Object> free = new Queue<Object>();
4
5 public ObjectPool() {}
6
7 public Object getObject() {
8 Object o = free.poll();
9 if (o == null) {
10 o = new Object();
11 }
12
13 inuse.add(o);
14
15 return o;
16 }
17
18 public void releaseObject(Object o) {
19 inuse.remove(o);
20 free.add(o);
21 }
22 }
1 public void boxMe(Integer in) {
2 System.out.println(in);
3 }
4
5 public void unboxMe(int in) {
6 System.out.println(in);
7 }
8
9 Integer unbox = new Integer(1);
10 int box = 1;
11
12 boxMe(box);
13 unboxMe(unbox);
The call to boxMe
effectively compiles to this:
1 int box = 1;
2 Integer _box = new Integer(box);
3 boxMe(_box);
The call to unboxMe
effectively compiles to this:
1 Integer unbox = new Integer(1);
2 int _unbox = unbox.intValue();
3 unboxMe(_unbox);
In both cases, additional overhead is incurred; boxing is way more expensive
split: creates N new strings, several regex objects, and can't be made to not use regular expressions.
concat: this is the same as "a" + "b". Compiles to a new StringBuilder object with tons of append calls. This is partially optimized but brutal in tight loops.
Threads are expensive
Use thread pools instead -- you can just object pool those
You can't use actual Thread objects with this
Use FutureTask instead
final
and static final
variables can be and are inlined by the
compiler and are well worth using
final
methods can be inlined by the JIT at runtime, but probably
aren't worth using unless there's an identified bottleneck
50% took less than 11 seconds; max 36 seconds (1000 reqs, 32 concurrency)
66% took less than 6 seconds; max 12 seconds (1000 reqs, 32 concurrency)
Table of Contents | t |
---|---|
Source Files | s |
Slide Numbers | n |
Notes | 2 |
Help | h |