深入理解 synchronized 关键字

如果某一个资源被多个线程共享,为了避免因为资源抢占导致资源数据错乱,我们需要对线程进行同步,那么synchronized就是实现线程同步的关键字,是用来保证被锁定了代码同一时间只能有一个线程执行,那么synchronized关键字的实现原理是怎样的呢?

先给出一张图,看看 synchronized 这个关键字有多复杂,先看看能看懂多少,如果看不太懂,希望看完本文能真正理解 synchronized 关键字和这张图。

Read more

Understanding Object Oriented Programming

这是一篇很早的文章,讲的关于面向对象,原文地址:Understanding Object Oriented Programming。这里有关于这篇文章的评论如此理解面向对象编程,很有有趣。我觉得这篇作为一篇入门讲面向对象的例子还是很不错的,通过不同的例子讲述了不同人的实现想法。最后用策略模式+工厂模式来实现,来达到消除if-else。

The code on this page grew out of a discussion on the Object Technology in Computer Science Education list server. The discussion had been going on for about 36 hours in late March and early April 2000 centered on the question of “What is OO really; is it a real paradigm, different from procedural programming or is it just a packaging mechanism for procedural programming?” Both of the authors believe that it is a real paradigm shift, requiring a change in mental model in the practitioners. Winder produced the first three of the following code fragments to show the difference in styles between hackers, procedural programmers, and (naive) object oriented programmers. Bergin then added the more sophisticated OO version that appears last.

Read more