SlideShare a Scribd company logo
1 of 59
Download to read offline
Everything You Were Taught
About Java Is Wrong
Tim Boudreau
http://timboudreau.com
@kablosna
Questions You Probably Don't Ask
●
Do you ever wonder
– If DNS still works today?
– If your files are really files?
– If binary-search works the same today as yesterday?
– If grep will give the same results today as yesterday?
– If your compiler will produce the same thing with the same input?
●
Why not?
Moving The Industry Forward
●
Software moves forward via things that
– Can be finished
– You can rely on – they work and keep working
– Solve one problem – composable things
– Are efficient – do as little as the problem requires
– Have as few possible failure modes as possible
Q&A
What stops people from
writing things like that?
To Understand Java Today
●
It helps to understand a science fiction
movie from 1979
To Understand Java Today
●
In the late 20th century, Earth launches
a small, useful thing
To Understand Java Today
●
It gets discovered by aliens...
●
They are nice aliens...
●
They try to help Java on its mission
To Understand Java Today
●
Mostly they just make it bigger
To Understand Java Today
(A LOT bigger)
To Understand Java Today
Deep inside is still this small useful thing
To Understand Java Today
●
In the late 20th century, Java is launched
●
It is a small useful thing
To Understand Java Today
●
It gets discovered by enterprises...
●
And people who make money from
enterprises...
●
They try to help and improve it
To Understand Java Today
●
Mostly they just made it bigger
Where We Went Wrong
●
Premature standardization
– Nobody knew what the web would be in 1999
●
Marketecture
– Your bean wakes up and says 'Hey! I'm in a word processor!' - say what?
●
Componentization at the wrong level
– Objects are too small a unit of reuse
●
Grabbing things that were lying around and calling it architecture
– Threads for I/O? Really?
●
Making everything a nail, so all you need is a hammer
– It's EJBs all the way down!
Q&A
JavaBeans Are an
Antipattern
JavaBeans Are An Antipattern
●
JavaBeans were created for GUI builders
– Need to be mutable to change what the UI shows
– Completely wrong for modelling data unless it really must change
– If it must change, there are probably things that change together
– Look for objects with the same lifecycle
– Most of the time, replacing the whole data model is cheap
●
Setters are evil
– If it should not change after construction time, don't make that possible
– If you make it possible, you must assume forever that somebody is calling it
●
Overridable setters are worse evil
– Can be called from super constructor, before object is initialized
JavaBeans Are An Antipattern
●
Terrible problems with this:
public class Bean {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
●
The value is null by default, and no guarantee that it won't be
– All users must check, or you assume your code is bug-free
●
Too much text, too little utility
– Not thread-safe
– Verbose: Two words have meaning: “String value“. The rest is unnecessary noise
– It is not nice to humans to make them write (or read) noise-code
JavaBeans Are An Antipattern
●
This is actually worse:
public class Bean {
private String value;
public String getValue() {...}
public void setValue(String value) {this.value=value;}
public boolean equals (Object o) { return o instanceof Bean ? Objects.equals(value, ((Bean)
o).value) : false; }
public int hashCode() { return value == null ? 0 : value.hashCode(); }
}
●
Now the object's identity can change on the fly
– In a Set, very bad things can happen
●
hashCode() can throw a NullPointerException
– Another thread nulls value between the value==null test and value.hashCode()
JavaBeans Are An Antipattern
●
But wait, it gets worse!
public class Bean {
private String value;
public synchronized String getValue() { //this makes it thread-safe, right?
return value;
}
public synchronized void setValue(String value) {
this.value = value;
}
public boolean equals(){...} public int hashCode() {...}
public String toString() { return getValue(); };
}
●
toString() calls a synchronized method. Guess what can deadlock?
Logger.log (Level.FINE, “Look here {0}“, new Object[] { bean });
● This is not a theoretical problem! Just use JDK's (evil) FileLogger...
JavaBeans Are An Antipattern
Or you could make it as simple as it should be...
public class ImmutableBean {
public final String value; // yes, public. Getters are not magic!
public ImmutableBean(String value) {
assert value != null; // limit its state to having a value
this.value = value;
}
}
...and the problems disappear
Q&A
Inheritance Breaks Your
Brain
Inheritance Breaks Your Brain
●
Say you like coffee
Inheritance Breaks Your Brain
●
Say you like coffee
●
And toast
Inheritance Breaks Your Brain
●
Say you like coffee
●
And toast
●
And inheritance
– ToasterCoffeePot!
Inheritance Breaks Your Brain
●
What you need is something like this
Inheritance Breaks Your Brain
●
But people get taught
to build the
ToasterCoffeePot
instead
Inheritance Breaks Your Brain
●
You get used to thinking in is-a relationships
●
You stop thinking about has-a relationships
– You forget that has-a is usually more powerful
●
Building logic up out of subclasses is seductive
– Until you need to use one piece of logic without the rest
– You really have two things pretending to be one
Inheritance Breaks Your Brain
We have a parent project and a child project. The child project is a customization of the parent project, where some
of the interfaces in parent project will have different implementation classes. What I was thinking that we could do
was to subclassing a Module as:
class MyModule extends AbstractModule {
protected void configure() {
bind(Interface1.class).to(Class1.class);
bind(Interface2.class).to(Class2.class);
...
}
}
class SubModule extends MyModule {
protected void configure() {
super.configure();
bind(Interface2.class).to(Subclass2.class);
}
} http://j.mp/1j504Kt
Q&A
Application Servers are a
Historical Relic
Application Servers
●
1995 – NetDynamics – First Java Application Server
●
Typical desktop PC has 8Mb of memory
●
Your server might have 64Mb (if you're lucky)
●
1999 – Java EE 1.2
●
Problem - Java wants a lot of memory
– Optimization: Multiple web applications in a single Java process
Application Servers
●
Try to solve all the world's problems
– Most of which you don't have
– Every piece of code on a server is a potential security hole
●
Allow you to deploy multiple applications in a single Java process
– Great idea if you only have 64Mb of memory
– Do you only have 64Mb of memory?
●
Large memory footprint (~100Mb)
●
You get tons of technologies you don't use
Q&A
You don't need an ORM
(and you might not need a relational database)
The Magic Hard Drive In The Sky
●
Are you using a relational database?
●
Are you looking everything up by primary key?
●
Then you don't need one.
Do you really need an ORM?
●
Violates the DRY (don't repeat yourself) principle
– A Java class is a schema for data
– A database schema is a schema for data
●
Modern web applications are usually converting rows objects→ →
JSON
– Entity classes = validation?
●
SQL is not hard
– If you understand sets, you understand SQL
– For anything complex, you will need it anyway
Q&A
Threads for I/O? Really?
Threads for I/O? Really?
●
It's 1982.
●
You press a key on the keyboard
●
What happens?
– A hardware interrupt is generated
– The CPU stores the register contents and
– Jumps to the interrupt handler code
●
Why?
– Because the computer might be doing
something else
Threads for I/O? Really?
●
It's 2014.
●
You press a key on the keyboard
●
What happens?
– A hardware interrupt is generated
– The CPU stores the register contents and
– Jumps to the interrupt handler code
●
Why?
– Because the computer might be doing
something else
Threads for I/O? Really?
●
Why does it work this way?
●
It's fast and efficient
●
It's simple
– Hollywood principle – don't call us, we'll call you
●
It's reality
– A network packet shows up.
– You didn't know it would show up at that time.
– But you have to deal with it.
Threads for I/O? Really?
●
It's 1990
●
You want to read a file from disk
●
What happens?
– Turn on the disk motor
– Time the LED shining through the hole in the
disk
– Move the head
– Read some data into a memory buffer
– Process it
Threads for I/O? Really?
●
Why does it work this way?
– Because the computer only does one thing at a time
– It can only run one program at a time
– It couldn't possibly have anything else to do but wait for motors to move things
Is that true of any computer you use?
No. Not even your phone.
Threads for I/O? Really?
●
1990s. Networking for PCs is getting important.
●
Preemptive multitasking is here.
●
OS's get threads.
– Threads are great for parallel computation
– Programs get threads
●
Multitasking lets you program as if your program were the only
thing using the computer
– But it's not.
Threads for I/O? Really?
●
2000s. File I/O in a modern OS:
– Program asks to read a file
– OS puts the calling thread to sleep
– Goes and gets the data
– Wakes up the program thread
This is a model designed for a single-user, single-process computer.
WTF?
Threads for I/O? Really?
●
What are we really trying to do?
– Respond when an unexpected I/O event happens – network I/O
– Run X with the data in some file
– Run X with the data from some network connection
●
What's interesting is the work we want to do
●
What's NOT interesting is managing the plumbing that gets the data
Threads for I/O? Really?
●
You have an amazing, efficient I/O scheduler
●
And it's in your OS's kernel!
●
Managing the relationship of threads and I/O in the application layer
makes it less likely that you'll use it effectively
– And makes it very hard to change your threading model
Threads for I/O? Really?
●
Threads are a great abstraction for parallel work
●
They were lying around when Java was invented
●
Thread + pretending it's the 1980s is a terrible way to do I/O –
wrong tool for the job
●
Java NIO helps some
– Frameworks like Netty take it further
– You still have to fake async file I/O with thread pools
– But the entire stack needs to be async
Q&A
java.util.not-so-concurrent
Hide Your Locks, Hide Your Threading Model
●
java.util.concurrent is full of semi-useful things
●
Most of them are about how to block threads
– I.e. they are tools for limiting concurrency
– Once baked into a codebase, very expensive to remove
●
The more a lock is used, the more chance someone is using it wrong
Hide Your Locks, Hide Your Threading Model
●
A simple pattern to hide your locks:
public interface Receiver<T> {
void receive (T obj);
void failed (Throwable t);
}
●
Do your locking right, in one place, then call the receiver
– If paranoid, take steps to ensure T can't be passed outside the Receiver or used if it is
●
You can replace or eliminate the threading/locking model at any time
●
It's usually better to eliminate the reason for a lock if you can
Hide Your Locks, Hide Your Threading Model
●
Hide your Futures too
– Future is a way to block while background work happens
– ...it's a way to do background work...in the foreground!
●
If you expose a Future, you're offering the caller a chance to block
– Like saying 'Please use this wrong!'
– The only useful feature is cancel() - expose it another way:
public interface Cancellable {
public void cancel();
}
Q&A
Avoid Big Things
Avoid Big Things
●
Libraries that try to do everything
usually don't do everything well
●
Many types of bug come from
thinking you have one problem when
you have more than one
Q&A
What can we do?
Threads for I/O? Really?
●
Demand true asynchronous APIs for things like JDBC
– Throwing things in a thread pool != asynchronous, it's a hack-around
– But for some things it's the best we have for now
●
Stop using thread-per-socket frameworks
– It doesn't scale
JavaBeans Are An Antipattern
●
Avoid mutable state as much as possible
●
Use 'final' any place you can
– You can always un-final, but it's incompatible to go the other way
– You can't take salt out of the soup
Avoid Big Things
●
Choose libraries that solve one problem per-library
●
Then you can combine the best with the best
– And replace pieces when new things become available
●
Don't trust things that try to save the world
Application Servers
●
Use small frameworks that only do what you need
●
Merge JARs if necessary – your server is a program; you should be
able to run it with java -jar
Threads for I/O – Really?
●
Hide your threading model
– That way, you can change it
●
Avoid thread-per-socket frameworks
●
Avoid frameworks that expose ThreadLocals for managing state
– This will not work in an async world – they will break compatibility or die
– Yes, this means almost every Java EE web framework
– But server-side HTML generation is dead anyway
●
Look at async frameworks like Netty, Vert.X, Acteur, Akka.io
Inheritance Breaks Your Brain
●
Design small classes with immutable data
– The ideal interface has one method
– The ideal class has only final fields
●
Think of composing small parts, not things that can be other things
– Don't create the ToasterCoffeePot!
– If you wouldn't do it in the physical world, don't do it in software
●
An overlap in functionality = a shared dependency
– Decompose problems into small units
Q&A
Questions?

More Related Content

What's hot

[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...
[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...
[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...Seunghun Lee
 
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부Eunseok Yi
 
MVC Architecture from Maintenance Quality Attributes Perspective
MVC Architecture from Maintenance Quality Attributes PerspectiveMVC Architecture from Maintenance Quality Attributes Perspective
MVC Architecture from Maintenance Quality Attributes PerspectiveCSCJournals
 
게임밸런싱과 머신러닝, 활용사례 & Tensorflow
게임밸런싱과 머신러닝, 활용사례 & Tensorflow게임밸런싱과 머신러닝, 활용사례 & Tensorflow
게임밸런싱과 머신러닝, 활용사례 & Tensorflow현철 허
 
AngularJS - Présentation (french)
AngularJS - Présentation (french)AngularJS - Présentation (french)
AngularJS - Présentation (french)Yacine Rezgui
 
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인강 민우
 
Machine learning for Netflix recommendations talk at SF Make School
Machine learning for Netflix recommendations talk at SF Make SchoolMachine learning for Netflix recommendations talk at SF Make School
Machine learning for Netflix recommendations talk at SF Make SchoolFaisal Siddiqi
 
NDC2013 - 심리학으로 다시 보는 게임 디자인
NDC2013 - 심리학으로 다시 보는 게임 디자인NDC2013 - 심리학으로 다시 보는 게임 디자인
NDC2013 - 심리학으로 다시 보는 게임 디자인Jubok Kim
 
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성TaeSeong Lee
 
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들MinGeun Park
 
Fun QA_재미지수_GENII
Fun QA_재미지수_GENIIFun QA_재미지수_GENII
Fun QA_재미지수_GENII원철 정
 
모바일 게임기획 따라하며 배우기
모바일 게임기획 따라하며 배우기모바일 게임기획 따라하며 배우기
모바일 게임기획 따라하며 배우기Sunnyrider
 
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019devCAT Studio, NEXON
 
Keynote: Bias in Search and Recommender Systems
Keynote: Bias in Search and Recommender SystemsKeynote: Bias in Search and Recommender Systems
Keynote: Bias in Search and Recommender SystemsCatalyst
 
Informatique et sécurité du système d'information : guide de bonnes pratiques...
Informatique et sécurité du système d'information : guide de bonnes pratiques...Informatique et sécurité du système d'information : guide de bonnes pratiques...
Informatique et sécurité du système d'information : guide de bonnes pratiques...polenumerique33
 
Scub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libreScub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libreStéphane Traumat
 
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정강 민우
 
게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개Seungmo Koo
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 gamesErnesto Jiménez
 

What's hot (20)

[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...
[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...
[Devsisters] 세계 선도 IT사 및 게임사 벤치마킹 & 인사이트 보고서 (1부) 세계적 IT서비스 회사들의 성공의 본질을 해부하다...
 
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
NDC 2010 이은석 - 마비노기 영웅전 포스트모템 2부
 
MVC Architecture from Maintenance Quality Attributes Perspective
MVC Architecture from Maintenance Quality Attributes PerspectiveMVC Architecture from Maintenance Quality Attributes Perspective
MVC Architecture from Maintenance Quality Attributes Perspective
 
게임밸런싱과 머신러닝, 활용사례 & Tensorflow
게임밸런싱과 머신러닝, 활용사례 & Tensorflow게임밸런싱과 머신러닝, 활용사례 & Tensorflow
게임밸런싱과 머신러닝, 활용사례 & Tensorflow
 
AngularJS - Présentation (french)
AngularJS - Présentation (french)AngularJS - Présentation (french)
AngularJS - Présentation (french)
 
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인
[IGC2018] 캡콤 토쿠다 유야 - 몬스터헌터 월드의 게임 컨셉과 레벨 디자인
 
Machine learning for Netflix recommendations talk at SF Make School
Machine learning for Netflix recommendations talk at SF Make SchoolMachine learning for Netflix recommendations talk at SF Make School
Machine learning for Netflix recommendations talk at SF Make School
 
NDC2013 - 심리학으로 다시 보는 게임 디자인
NDC2013 - 심리학으로 다시 보는 게임 디자인NDC2013 - 심리학으로 다시 보는 게임 디자인
NDC2013 - 심리학으로 다시 보는 게임 디자인
 
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성
NDC 2015 게임 기획자에게 들려주는 괜찮은 이야기 _ 이태성
 
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들
[KGC2011_박민근] 신입 게임 개발자가 알아야 할 것들
 
Fun QA_재미지수_GENII
Fun QA_재미지수_GENIIFun QA_재미지수_GENII
Fun QA_재미지수_GENII
 
게임 디렉팅 튜토리얼
게임 디렉팅 튜토리얼게임 디렉팅 튜토리얼
게임 디렉팅 튜토리얼
 
모바일 게임기획 따라하며 배우기
모바일 게임기획 따라하며 배우기모바일 게임기획 따라하며 배우기
모바일 게임기획 따라하며 배우기
 
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019
이봄, 스토리텔링으로 즐기는 콘서트 - 시나리오 기획자를 위한 TRPG의 세계, NDC2019
 
Keynote: Bias in Search and Recommender Systems
Keynote: Bias in Search and Recommender SystemsKeynote: Bias in Search and Recommender Systems
Keynote: Bias in Search and Recommender Systems
 
Informatique et sécurité du système d'information : guide de bonnes pratiques...
Informatique et sécurité du système d'information : guide de bonnes pratiques...Informatique et sécurité du système d'information : guide de bonnes pratiques...
Informatique et sécurité du système d'information : guide de bonnes pratiques...
 
Scub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libreScub Foundation, usine logicielle Java libre
Scub Foundation, usine logicielle Java libre
 
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정
[IGC 2017] 넥슨코리아 심재근 - 시스템 기획자에 대한 기본 지식과 준비과정
 
게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 games
 

Similar to Everything You Were Taught About Java Is Wrong

Keeping the fun in functional w/ Apache Spark @ Scala Days NYC
Keeping the fun in functional   w/ Apache Spark @ Scala Days NYCKeeping the fun in functional   w/ Apache Spark @ Scala Days NYC
Keeping the fun in functional w/ Apache Spark @ Scala Days NYCHolden Karau
 
Big Data Beyond the JVM - Strata San Jose 2018
Big Data Beyond the JVM - Strata San Jose 2018Big Data Beyond the JVM - Strata San Jose 2018
Big Data Beyond the JVM - Strata San Jose 2018Holden Karau
 
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet
 
AMW43 - Unba.se, Distributed database for human interaction
AMW43 - Unba.se, Distributed database for human interactionAMW43 - Unba.se, Distributed database for human interaction
AMW43 - Unba.se, Distributed database for human interactionDaniel Norman
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?Colin Riley
 
Path dependent-development (PyCon India)
Path dependent-development (PyCon India)Path dependent-development (PyCon India)
Path dependent-development (PyCon India)ncoghlan_dev
 
PuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With NotesPuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With NotesPhil Zimmerman
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfjexp
 
Work Queues
Work QueuesWork Queues
Work Queuesciconf
 
DevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 SlidesDevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 SlidesAlex Cruise
 
How to get started with Site Reliability Engineering
How to get started with Site Reliability EngineeringHow to get started with Site Reliability Engineering
How to get started with Site Reliability EngineeringAndrew Kirkpatrick
 
CoreData - there is an ORM you can like!
CoreData - there is an ORM you can like!CoreData - there is an ORM you can like!
CoreData - there is an ORM you can like!Tomáš Jukin
 
Path Dependent Development (PyCon AU)
Path Dependent Development (PyCon AU)Path Dependent Development (PyCon AU)
Path Dependent Development (PyCon AU)ncoghlan_dev
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sampleprahalad_das_in
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem Claudson Oliveira
 
Hardware Prototyping & Hyper Island for XD Meetup Stockholm
Hardware Prototyping & Hyper Island for XD Meetup StockholmHardware Prototyping & Hyper Island for XD Meetup Stockholm
Hardware Prototyping & Hyper Island for XD Meetup StockholmSune Kaae
 

Similar to Everything You Were Taught About Java Is Wrong (20)

API Design
API DesignAPI Design
API Design
 
Cloud accounting software uk
Cloud accounting software ukCloud accounting software uk
Cloud accounting software uk
 
Keeping the fun in functional w/ Apache Spark @ Scala Days NYC
Keeping the fun in functional   w/ Apache Spark @ Scala Days NYCKeeping the fun in functional   w/ Apache Spark @ Scala Days NYC
Keeping the fun in functional w/ Apache Spark @ Scala Days NYC
 
Big Data Beyond the JVM - Strata San Jose 2018
Big Data Beyond the JVM - Strata San Jose 2018Big Data Beyond the JVM - Strata San Jose 2018
Big Data Beyond the JVM - Strata San Jose 2018
 
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
 
AMW43 - Unba.se, Distributed database for human interaction
AMW43 - Unba.se, Distributed database for human interactionAMW43 - Unba.se, Distributed database for human interaction
AMW43 - Unba.se, Distributed database for human interaction
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Path dependent-development (PyCon India)
Path dependent-development (PyCon India)Path dependent-development (PyCon India)
Path dependent-development (PyCon India)
 
PuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With NotesPuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With Notes
 
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
 
Work Queues
Work QueuesWork Queues
Work Queues
 
DevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 SlidesDevOps Days Vancouver 2014 Slides
DevOps Days Vancouver 2014 Slides
 
How to get started with Site Reliability Engineering
How to get started with Site Reliability EngineeringHow to get started with Site Reliability Engineering
How to get started with Site Reliability Engineering
 
CoreData - there is an ORM you can like!
CoreData - there is an ORM you can like!CoreData - there is an ORM you can like!
CoreData - there is an ORM you can like!
 
Path Dependent Development (PyCon AU)
Path Dependent Development (PyCon AU)Path Dependent Development (PyCon AU)
Path Dependent Development (PyCon AU)
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sample
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem
 
Hardware Prototyping & Hyper Island for XD Meetup Stockholm
Hardware Prototyping & Hyper Island for XD Meetup StockholmHardware Prototyping & Hyper Island for XD Meetup Stockholm
Hardware Prototyping & Hyper Island for XD Meetup Stockholm
 

Recently uploaded

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Recently uploaded (20)

ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

Everything You Were Taught About Java Is Wrong

  • 1. Everything You Were Taught About Java Is Wrong Tim Boudreau http://timboudreau.com @kablosna
  • 2. Questions You Probably Don't Ask ● Do you ever wonder – If DNS still works today? – If your files are really files? – If binary-search works the same today as yesterday? – If grep will give the same results today as yesterday? – If your compiler will produce the same thing with the same input? ● Why not?
  • 3. Moving The Industry Forward ● Software moves forward via things that – Can be finished – You can rely on – they work and keep working – Solve one problem – composable things – Are efficient – do as little as the problem requires – Have as few possible failure modes as possible
  • 4. Q&A What stops people from writing things like that?
  • 5. To Understand Java Today ● It helps to understand a science fiction movie from 1979
  • 6. To Understand Java Today ● In the late 20th century, Earth launches a small, useful thing
  • 7. To Understand Java Today ● It gets discovered by aliens... ● They are nice aliens... ● They try to help Java on its mission
  • 8. To Understand Java Today ● Mostly they just make it bigger
  • 9. To Understand Java Today (A LOT bigger)
  • 10. To Understand Java Today Deep inside is still this small useful thing
  • 11. To Understand Java Today ● In the late 20th century, Java is launched ● It is a small useful thing
  • 12. To Understand Java Today ● It gets discovered by enterprises... ● And people who make money from enterprises... ● They try to help and improve it
  • 13. To Understand Java Today ● Mostly they just made it bigger
  • 14. Where We Went Wrong ● Premature standardization – Nobody knew what the web would be in 1999 ● Marketecture – Your bean wakes up and says 'Hey! I'm in a word processor!' - say what? ● Componentization at the wrong level – Objects are too small a unit of reuse ● Grabbing things that were lying around and calling it architecture – Threads for I/O? Really? ● Making everything a nail, so all you need is a hammer – It's EJBs all the way down!
  • 16. JavaBeans Are An Antipattern ● JavaBeans were created for GUI builders – Need to be mutable to change what the UI shows – Completely wrong for modelling data unless it really must change – If it must change, there are probably things that change together – Look for objects with the same lifecycle – Most of the time, replacing the whole data model is cheap ● Setters are evil – If it should not change after construction time, don't make that possible – If you make it possible, you must assume forever that somebody is calling it ● Overridable setters are worse evil – Can be called from super constructor, before object is initialized
  • 17. JavaBeans Are An Antipattern ● Terrible problems with this: public class Bean { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } } ● The value is null by default, and no guarantee that it won't be – All users must check, or you assume your code is bug-free ● Too much text, too little utility – Not thread-safe – Verbose: Two words have meaning: “String value“. The rest is unnecessary noise – It is not nice to humans to make them write (or read) noise-code
  • 18. JavaBeans Are An Antipattern ● This is actually worse: public class Bean { private String value; public String getValue() {...} public void setValue(String value) {this.value=value;} public boolean equals (Object o) { return o instanceof Bean ? Objects.equals(value, ((Bean) o).value) : false; } public int hashCode() { return value == null ? 0 : value.hashCode(); } } ● Now the object's identity can change on the fly – In a Set, very bad things can happen ● hashCode() can throw a NullPointerException – Another thread nulls value between the value==null test and value.hashCode()
  • 19. JavaBeans Are An Antipattern ● But wait, it gets worse! public class Bean { private String value; public synchronized String getValue() { //this makes it thread-safe, right? return value; } public synchronized void setValue(String value) { this.value = value; } public boolean equals(){...} public int hashCode() {...} public String toString() { return getValue(); }; } ● toString() calls a synchronized method. Guess what can deadlock? Logger.log (Level.FINE, “Look here {0}“, new Object[] { bean }); ● This is not a theoretical problem! Just use JDK's (evil) FileLogger...
  • 20. JavaBeans Are An Antipattern Or you could make it as simple as it should be... public class ImmutableBean { public final String value; // yes, public. Getters are not magic! public ImmutableBean(String value) { assert value != null; // limit its state to having a value this.value = value; } } ...and the problems disappear
  • 22. Inheritance Breaks Your Brain ● Say you like coffee
  • 23. Inheritance Breaks Your Brain ● Say you like coffee ● And toast
  • 24. Inheritance Breaks Your Brain ● Say you like coffee ● And toast ● And inheritance – ToasterCoffeePot!
  • 25. Inheritance Breaks Your Brain ● What you need is something like this
  • 26. Inheritance Breaks Your Brain ● But people get taught to build the ToasterCoffeePot instead
  • 27. Inheritance Breaks Your Brain ● You get used to thinking in is-a relationships ● You stop thinking about has-a relationships – You forget that has-a is usually more powerful ● Building logic up out of subclasses is seductive – Until you need to use one piece of logic without the rest – You really have two things pretending to be one
  • 28. Inheritance Breaks Your Brain We have a parent project and a child project. The child project is a customization of the parent project, where some of the interfaces in parent project will have different implementation classes. What I was thinking that we could do was to subclassing a Module as: class MyModule extends AbstractModule { protected void configure() { bind(Interface1.class).to(Class1.class); bind(Interface2.class).to(Class2.class); ... } } class SubModule extends MyModule { protected void configure() { super.configure(); bind(Interface2.class).to(Subclass2.class); } } http://j.mp/1j504Kt
  • 29. Q&A Application Servers are a Historical Relic
  • 30. Application Servers ● 1995 – NetDynamics – First Java Application Server ● Typical desktop PC has 8Mb of memory ● Your server might have 64Mb (if you're lucky) ● 1999 – Java EE 1.2 ● Problem - Java wants a lot of memory – Optimization: Multiple web applications in a single Java process
  • 31. Application Servers ● Try to solve all the world's problems – Most of which you don't have – Every piece of code on a server is a potential security hole ● Allow you to deploy multiple applications in a single Java process – Great idea if you only have 64Mb of memory – Do you only have 64Mb of memory? ● Large memory footprint (~100Mb) ● You get tons of technologies you don't use
  • 32. Q&A You don't need an ORM (and you might not need a relational database)
  • 33. The Magic Hard Drive In The Sky ● Are you using a relational database? ● Are you looking everything up by primary key? ● Then you don't need one.
  • 34. Do you really need an ORM? ● Violates the DRY (don't repeat yourself) principle – A Java class is a schema for data – A database schema is a schema for data ● Modern web applications are usually converting rows objects→ → JSON – Entity classes = validation? ● SQL is not hard – If you understand sets, you understand SQL – For anything complex, you will need it anyway
  • 36. Threads for I/O? Really? ● It's 1982. ● You press a key on the keyboard ● What happens? – A hardware interrupt is generated – The CPU stores the register contents and – Jumps to the interrupt handler code ● Why? – Because the computer might be doing something else
  • 37. Threads for I/O? Really? ● It's 2014. ● You press a key on the keyboard ● What happens? – A hardware interrupt is generated – The CPU stores the register contents and – Jumps to the interrupt handler code ● Why? – Because the computer might be doing something else
  • 38. Threads for I/O? Really? ● Why does it work this way? ● It's fast and efficient ● It's simple – Hollywood principle – don't call us, we'll call you ● It's reality – A network packet shows up. – You didn't know it would show up at that time. – But you have to deal with it.
  • 39. Threads for I/O? Really? ● It's 1990 ● You want to read a file from disk ● What happens? – Turn on the disk motor – Time the LED shining through the hole in the disk – Move the head – Read some data into a memory buffer – Process it
  • 40. Threads for I/O? Really? ● Why does it work this way? – Because the computer only does one thing at a time – It can only run one program at a time – It couldn't possibly have anything else to do but wait for motors to move things Is that true of any computer you use? No. Not even your phone.
  • 41. Threads for I/O? Really? ● 1990s. Networking for PCs is getting important. ● Preemptive multitasking is here. ● OS's get threads. – Threads are great for parallel computation – Programs get threads ● Multitasking lets you program as if your program were the only thing using the computer – But it's not.
  • 42. Threads for I/O? Really? ● 2000s. File I/O in a modern OS: – Program asks to read a file – OS puts the calling thread to sleep – Goes and gets the data – Wakes up the program thread This is a model designed for a single-user, single-process computer. WTF?
  • 43. Threads for I/O? Really? ● What are we really trying to do? – Respond when an unexpected I/O event happens – network I/O – Run X with the data in some file – Run X with the data from some network connection ● What's interesting is the work we want to do ● What's NOT interesting is managing the plumbing that gets the data
  • 44. Threads for I/O? Really? ● You have an amazing, efficient I/O scheduler ● And it's in your OS's kernel! ● Managing the relationship of threads and I/O in the application layer makes it less likely that you'll use it effectively – And makes it very hard to change your threading model
  • 45. Threads for I/O? Really? ● Threads are a great abstraction for parallel work ● They were lying around when Java was invented ● Thread + pretending it's the 1980s is a terrible way to do I/O – wrong tool for the job ● Java NIO helps some – Frameworks like Netty take it further – You still have to fake async file I/O with thread pools – But the entire stack needs to be async
  • 47. Hide Your Locks, Hide Your Threading Model ● java.util.concurrent is full of semi-useful things ● Most of them are about how to block threads – I.e. they are tools for limiting concurrency – Once baked into a codebase, very expensive to remove ● The more a lock is used, the more chance someone is using it wrong
  • 48. Hide Your Locks, Hide Your Threading Model ● A simple pattern to hide your locks: public interface Receiver<T> { void receive (T obj); void failed (Throwable t); } ● Do your locking right, in one place, then call the receiver – If paranoid, take steps to ensure T can't be passed outside the Receiver or used if it is ● You can replace or eliminate the threading/locking model at any time ● It's usually better to eliminate the reason for a lock if you can
  • 49. Hide Your Locks, Hide Your Threading Model ● Hide your Futures too – Future is a way to block while background work happens – ...it's a way to do background work...in the foreground! ● If you expose a Future, you're offering the caller a chance to block – Like saying 'Please use this wrong!' – The only useful feature is cancel() - expose it another way: public interface Cancellable { public void cancel(); }
  • 51. Avoid Big Things ● Libraries that try to do everything usually don't do everything well ● Many types of bug come from thinking you have one problem when you have more than one
  • 53. Threads for I/O? Really? ● Demand true asynchronous APIs for things like JDBC – Throwing things in a thread pool != asynchronous, it's a hack-around – But for some things it's the best we have for now ● Stop using thread-per-socket frameworks – It doesn't scale
  • 54. JavaBeans Are An Antipattern ● Avoid mutable state as much as possible ● Use 'final' any place you can – You can always un-final, but it's incompatible to go the other way – You can't take salt out of the soup
  • 55. Avoid Big Things ● Choose libraries that solve one problem per-library ● Then you can combine the best with the best – And replace pieces when new things become available ● Don't trust things that try to save the world
  • 56. Application Servers ● Use small frameworks that only do what you need ● Merge JARs if necessary – your server is a program; you should be able to run it with java -jar
  • 57. Threads for I/O – Really? ● Hide your threading model – That way, you can change it ● Avoid thread-per-socket frameworks ● Avoid frameworks that expose ThreadLocals for managing state – This will not work in an async world – they will break compatibility or die – Yes, this means almost every Java EE web framework – But server-side HTML generation is dead anyway ● Look at async frameworks like Netty, Vert.X, Acteur, Akka.io
  • 58. Inheritance Breaks Your Brain ● Design small classes with immutable data – The ideal interface has one method – The ideal class has only final fields ● Think of composing small parts, not things that can be other things – Don't create the ToasterCoffeePot! – If you wouldn't do it in the physical world, don't do it in software ● An overlap in functionality = a shared dependency – Decompose problems into small units