I write Engineering Topic weekly...?

Blog of web engineer at Tokyo. [Japanese version] http://taichiw.hatenablog.com/

You should avoid using forEach on Java as much as possible, I think

Collection shouldn't be updated in lambda explanation

Sometimes I see these kind of code.

List<X> list = new ArrayList<>();
/*AnyCollection*/.forEach()
  .map(t -> list.add(t.getXXXX()));

I think it's bad code.

It's not thread safe

If you use parallel stream on above code...

List<X> list = new ArrayList<>();
/*AnyCollection*/.forEach().parallelStream()
  .map(x -> list.add(x.getXXXX()));

it doesn't work correctly because ArrayList's add is not thread safe. "Sometimes" strange null will be added in the list. *1

Of course, you can avoid this trouble if you don't use parallel stream.
However, there is no assuarance that nobody change this part to parallel in the future.

It's not "Function"

Lambda explanations accept only final (or actually final) variable from out of lambda. So any variables doesn't accept new value.*2
On the other hand, "update" isn't restricted like "add to list" or setter.

However, even it's not restricted, we shouldn't do any update against variable, which is defined out of lambda.

Idiom for making collection is existing

This is most common way to make list.

List<X> list = /*AnyCollection*/.stream() //parallel stream can be used
  .map(X::getXXXX)
  .collect(Collector.toList());

This format shows clearly that you want to make list better than previous sample.

Finally, forEach can use for just output

If you keep "rule that you cannot update any external value", only output, like logging, will be the role of forEach.

Then... I think you should avoid using forEach on Java as much as possible.

*1:"Sometimes" is very tough. It's difficult to find this issue by test

*2:It will be compile error

Any System's behavior can be described by only "Input" & "Output"

Even it's any size or any type, system's behavior can be described by only "Input" and "Output'.
f:id:taichiw:20190118235359p:plain

Subroutines in program (function, method, ...)

The behavior can be described by requested parameter and returned value.

public String greet (String name){
    return "Hello " + name + "!";
}

Each services in microservice architecture

It can be explained by API's request and response.
f:id:taichiw:20190119000332p:plain

Whole system's behavior on microservice architecture

With perspective that whole system is one of huge black box, we can explain that by the published API's request and response.
f:id:taichiw:20190119000823p:plain
f:id:taichiw:20190119000848p:plain

Web page

Possible to explain by URL (and posted contents) as request and returned HTML and some contents as response.
f:id:taichiw:20190119001413p:plain

But... does some system update the data, doesn't it?

Yes, we need say "the status is updated" for describing the behavior if we think this "System" is consists from one service and database.
f:id:taichiw:20190119002052p:plain

But with another perspective, if we think only the service is system... then we can say, the request to Database is also just one of output from the service.
f:id:taichiw:20190119002118p:plain

In that way, we can say this system just has two kind of output.

We can adapt same idea to subroutine on program.

class Person {
  String name;
  public setName(String name) {
    this.name = name;
  }
}

We can understand this code like this...
f:id:taichiw:20190119002532p:plain

but this is also possible.
f:id:taichiw:20190119002715p:plain

The case get data from database

f:id:taichiw:20190119002824p:plain
In this case, we can say just this service has two kind of input.

1 million threads are executable in parallel with "Fiber" !? - Project Valhalla & Project Loom : JavaDayTokyo 2018 Report 2 #JavaDayTokyo

I attended sessions about Project Valhalla and Project Loom. Speaker of both sessions was Mr. David Buck *1.
Unfortunately, both functionality will not be in Java 11. Then, we can use them in actual service in 2021 *2 in the earliest case.

Project Valhalla

Value Types

Here is a class which has primitive type fields.

class Point {
   int x;
   int y;
}

Then, if we make these kind of array

  Point[] points;

each Point instances have their headers. Those will be huge overhead on memory.

This better if we think only memory optimization but it's not cool on object oriented programming.

  int[] xList;
  int[] yList;

So, here is one of solution*3.
If the class is declared with "value" like this, the memory allocation behave similarly to "primitive array".

value class Point {
   int x;
   int y;
}

Then, not only optimizing memory allocation, also improving speed for referencing each elements becomes possible.


Generic Specialization

In addition, the gap between primitive and class will be removed more.

For example, this declaration becomes possible.

ArrayList<int> xList;

In other example, we don't need "special implementation for primitive" like intStream anymore.
It's "not cool" that only primitive type variable requires special method.

Project Loom

On current JVM architecture, threads on JVM are mapped with threads managed by OS. The purpose of each threads might be different. Some of them might be used for very heavy process like encryption. On the other hand, some of them might be used for very light process, like just updating any variable. But OS need to make "thread, it's possible to use anything" because OS doesn't know what is the use for each threads.

Then, new idea "Fibers" was introduced. Fibers are threads, which are managed by run time of Java or user's code, not by OS. Application can manage threads (= Fibers) much better than OS because it knows how those threads are used. Surprisingly, JVM can generate thousand times threads than OS.

Blocking vs asynchronous

The traditional "thread way" is weak for scaling. Even if that thread is just waiting I/O, those are need to be kept on memory. As solution against this issue, "asynchronous" is used. But this way is difficult to coding / debugging. It's much more complex than "thread way".

"Fibers" can resolve these issues at once. The program can be kept simple because Fibers are just threads from program's perspective. On the other hand, it's strong for scalability too.

"Next era!"

Again, Fibers are threads. So each fibers has information "what will be executed" and "what has been executed". At the same time, those are objects managed by Java. Thus, they are "seriallizable".

Then... these will be possible.

  • Serialize and save a running thread. Next, stop JVM. Later JVM re-start and continue the saved thread.
  • On distributed database, transfer a process (= a fiber) between servers.

The current development stage

Right now, the development is on very early stage. Nothing is committed yet to remote repository because Developers just enjoy on their local machine lol
In the best case, any early access will be released within this year.

Anyway, this is very exciting improvement of Java. I'm looking forward to the release.

*1:He also ran demo of Fright Recorder in the keynote session. He looked so busy

*2:next LTS version is 17, will be released in 2021

*3:"How to write" is still under discussion. It's NOT fixed yet

Java11 : JavaDayTokyo2018 report 1 #JavaDayTokyo

I've attended to this year's JavaDayTokyo.

My personal themes was learning about these.

  • Java11, the LTS version being released half years after
  • Other upcoming functions
    • Especially about Project Valhalla. Recently, "How to treat huge size collection" is one of my important topic in my work.

Update of Java 11 from Java 10

These 8 JPEs will be added.
http://openjdk.java.net/projects/jdk/11/

From them, the only update to "how to write code" looks this.
JEP 323: Local-Variable Syntax for Lambda Parameters

So, I think I can say the difference between Java 8 and Java 11 is "Jigsaw + 'var'".

The "Next" LTS version is 17

The next LTS version after 11 is 17, it will be released on September, 2021. It is 3 years after than 11's release.
(I had thought LTS is released per 1.5 years, but not)

Mission Control and Flight Recorder will be free, from JDK 11

From JDK 11, we can use Mission Control & Flight Recorder for free.
In key note session, the demonstration of them was held. It looked so useful for finding reason of performance issue.

I wanna use it for my huge & complex product !

"Align function" on PowerPoint

Power Point has "Align function". It's on very hidden place, but it's very helpful for formatting.

This is example of using "Distribute Horizontally" and "Align Left"

f:id:taichiw:20180225114317p:plain

 

"The God of presenation", Mr. Madoka Sawa said, when he gets new computer, first he adds these function into "Quick access tool bar"

f:id:taichiw:20180225113607p:plain

I heard that in this session. (sorry it's written in Japanese)

taichiw.hatenablog.com

Regional Scrum Gathering® Tokyo 2014, Keynote speech by Ms. Jutta Eckstein #sgt2014

At 2nd day of Regional Scrum gathering Tokyo 2014, the keynote speech was "Introducing Agility into an Organization or: How to become Agile" by Jutta Eckestein.

This session gave me some great hints. They will help me to introduce something new to my team. This can be adapted to not only agile, also any other things.

Now, I'm trying to let our team members use TDD. So, this session was so useful for that. 

Typical Step of Change

She introduced 2 type pycological model about "Change". Elisabeth Kübler-Ross's one and Virginia Satir's one.

Jutta explained both of them with example of PowerPoint2010.

Elisabeth Kübler-Ross's model

  1. "I don't like UI of new PowerPoint. I'll continuously use old version"
  2. "I have to use new one."
  3. "I used to use new one. I don't hate new one anymore"

Virginia Satir's model

  1. Comfortable. "I like to use old PowerPoint".
  2. Chaos. "Sometimes I feel new one is good, but sometimes feel that is bad"
  3. Make sense. Understanding new one.
  4. Integration & Practice

We should understand  these "change" will cause in persons who are introduced something new.

How to introduce new things

Jutta told us 7 steps to introduce new things.

  1. Preparation (Explaning what we will try)
  2. Retrospective
  3. Readiness/Enabling Workshop
  4. (Customized) Training
  5. Monitoring / Coaching
  6. Sustaining Change
  7. Leassons Learned

Especially I was got strong impression from 3. and 4. So I'll explain them.

Readiness/Enabling Workshop

Classify "new things" to these 4 classes.

  1. What's we already do
    -> We might do some one already. For example, we don't call it "daily scrum", but we do morning meeting every day.
  2. What's easy to apply
  3. What's really difficult to apply
  4. What's impossible to do

Not only classify, also it's important to be clear what is blocker to do them.

In our team's case (starting TDD), No.1 is "Writing test code". Although the timing is after writing production code, anyway, all of us write test code.

(Customized) Training

After we decided to do something new, we will do training of that. But we should not create virtual issue for just the training because we cannot learn the skill to real issue by that.

We should "customize" training, which follow real issue.

3 Roles

Jutta also said, 3 roles should work together to start new things.

  • Passinate change agent
    -> The person who has passion to change something.
  • Project Leader
    -> He / She can construct relation with manager.
  • Architect / Technical leader
    -> The person who has high skill / knowleadge about the team's product. 

I think I'm so happy because now, my team's "Project Leader" & Architect are so collaborative.

 

Conclusion

 I got great hint to introduce something new to my team! I'm going to use these knowledge to adapt something new.

JavaOne2013 Overview : JavaOne2013 report 2 #JavaOne

This is overview report of JavaOne2013. I'll post each session's repot to here.

f:id:taichiw:20130927055138j:plain

 

Why did I attend JavaOne?

In this year, I'm trying increasing my "technical skill", especialy programing skill. So, I tryed to attend technical conference about programing language.

What did I noticed when I choose sessions?

I had heard there are so many sessions in Java, but I was realy surprised that I need choose my sessions from huge number of them.

f:id:taichiw:20131001055904p:plain

Through choosing my sessions, I found I was interested in REST, cloud, and operation. They were related my current tasks.

Since "Java technology" is so wide, and I didn't know which kind of technology was I interested in. But thanks to this "choosing", I could know my favor. This is one of what I learned.

What did I get / know / notice

The most impressive learning from JavaOne is that I realized that I have so many chance to consider and chose components consisting application.
In addition, I also noticed an importance of "practice swing" for choosing them.

Example1. choosing application server

"CON4117:The Adventurous Developer's Guide to Application Servers" gave me great learning.

In truth, I have not thought about application server deeply. I've used tomcat without thinking. But now, I know there are many kind of application servers, in addition, some of them were released in recent years. Now, I can consider which one is the best to each applications which have different strong point.

Example2. considering using Java8

About language, it is good experience that I imagined start of using Java8 on our work. By keynote and some session like "Programing with Lambda Expressions in Java", I could realize that the change to Java8 is so big, that we should adjust to this for readable code, and that every team members (of course, containing me) should learn it preliminarily to start using easily.

Attending overseas conference is realy bang for the buck?

Can't I realize such things without going overseas conference?
This attendance costed my company almost as same as my monthly salary.

Even so, I think going JavaOne is U.S. gave me such graet things.

Motivation of studing English

Since I could not understand each session well from my poor English skill

Realization my interest from tons of session theme

As I already told it, I could find my interest by chosing my session from unbelievable number of sessions.

Time for "practice swing". Training camp

In truth, I spent more time to "looking back" of sessions than hearing them. It means I sit down near the outlet, and learned more about session's theme, wrote program code, and posted blog.

It seems these things don't require going abroad. But actually it's very difficult to create enough time for that in daily life. Thanks to I was in another country, I could concentrate only technology. I thought it is like training camp of a sport.

 

This time, I could get those realization. I deeply appreciate this global opportunity.