第7章高水準プログラミング言語
Implementing Synchronization同期の実装
Synchronization must be carefully implemented to avoid situations that synchronization itself can cause:
英語のヒント
同期そのものが引き起こしかねない次のような状況を避けるため、同期は慎重に実装しなければならない。
• starvation: when a process must wait to enter a critical section, but the other processes monopolize the section, and the waiting process doesn’t get processor time.
英語のヒント
• 飢餓状態:プロセスがクリティカルセクションに入るのを待たなければならないのに、ほかのプロセスがその領域を独占し、待っているプロセスにプロセッサー時間が与えられない状態。
• busy waiting: when a process continually looks to see if it has access to a critical section, taking processor time from all processes.
英語のヒント
• ビジーウェイト:プロセスがクリティカルセクションにアクセスできるかを絶えず確認し続け、全プロセスが使うプロセッサー時間を消費する状態。
• deadlock: when multiple threads need a critical section that is being monopolized by one of the processes.
英語のヒント
• デッドロック:複数のスレッドが、あるプロセスに独占されているクリティカルセクションを必要とする状態。
Java is known for its strong implementation of synchronization. When marked as synchronized, it is guaranteed that it can have only one thread executing in it at the same time. Any other thread trying to enter the method or code block is blocked until the running occurrence exits the method or block. The following Java code shows the syntax of using the keyword on a method:
英語のヒント
Javaは、強力な同期の実装で知られている。synchronizedを付けると、同じ時点でその中を実行できるスレッドは一つだけだと保証される。そのメソッドやコードブロックに入ろうとするほかのスレッドは、実行中の処理がそこから出るまでブロックされる。次のJavaコードは、このキーワードをメソッドに使う構文を示す。
public synchronized void calcAverage() {
// all of the method code
}A synchronized method is coordinated on the object that owns the method. In OOP each instance of the object may have its own synchronization. For example, we might have a Car class that owns a method called start(). Each car instance we build has its own copy of the method; therefore, if we are starting a car, no other process can start that particular car. But if we have more than one car, another one may be started concurrently.
英語のヒント
synchronizedメソッドの同期は、そのメソッドを持つオブジェクトを単位に行われる。OOPでは、オブジェクトの各インスタンスが、それぞれ独自に同期できる。たとえば、start()というメソッドを持つCarクラスを考える。作成した車の各インスタンスは、それぞれメソッドのコピーを持つ。そのため、一台の車を始動している間は、ほかのプロセスがその同じ車を始動させることはできない。しかし、車が複数あるなら、別の車は並行して始動できる。
The following Java code shows the syntax of using the keyword on a code block within a method:
次のJavaコードは、メソッド内のコードブロックに、このキーワードを使う構文を示す。
synchronized (this) {
// critical section
}This code will execute exactly as if it was a synchronized method. This keyword refers to the current object, so this code is also synchronizing on the object which owns the code block. This usage provides much more granularity and efficient execution times.
英語のヒント
このコードは、synchronizedメソッドであるかのように、まったく同じ動作をする。thisキーワードは現在のオブジェクトを指すので、このコードも、コードブロックを持つオブジェクトを単位として同期する。この使い方では、より細かい粒度で制御でき、実行時間も効率化できる。
Another powerful tool for synchronization in Java is to declare a whole class as a thread, meaning the whole class is within a single thread of execution. There are two ways to do this. The first is having the class inherit from the Thread class from the Java API:
英語のヒント
Javaの同期のための、もう一つの強力な手段は、クラス全体をスレッドとして宣言することである。これは、そのクラス全体が一つの実行スレッド内にあることを意味する。これには二つの方法がある。一つ目は、Java APIのThreadクラスを、そのクラスに継承させる方法である。
public class SomeThread extends Thread {
}The other way is to have the class implement an interface from the API:
もう一つは、APIのインターフェースを、そのクラスに実装させる方法である。
public class SomeThread implements Runnable {
}Either of these methodologies provides access to the useful methods of the Thread class which are inherited and can be overridden to support the particular situation. The interface forms a contract with the programmer to implement these methods to support the particular task.
英語のヒント
どちらの方法でも、Threadクラスの有用なメソッドにアクセスできる。それらは継承され、特定の状況に対応するようにオーバーライドできる。インターフェースは、個々の作業に対応するためにこれらのメソッドを実装するという、プログラマーとの契約を形作る。
A Thread object is controlled by a priority-based scheduler. Some of the most important methods at the programmer’s disposal are as follows:
英語のヒント
Threadオブジェクトは、優先度に基づくスケジューラーによって制御される。プログラマーが使える特に重要なメソッドには、次のようなものがある。
•
start(): causes the thread to begin execution• start():スレッドの実行を開始させる。
•
yield(): tells the thread scheduler that the current thread is willing to yield its control英語のヒント
• yield():現在のスレッドが、制御を譲る意思を持つことをスレッドスケジューラーに伝える。
•
sleep(int time): causes the currently running thread to temporarily hibernate for a specified number of milliseconds英語のヒント
• sleep(int time):現在実行中のスレッドを、指定したミリ秒数だけ、一時的に休止させる。
•
setPriority(int priority): changes the priority of the current thread• setPriority(int priority):現在のスレッドの優先度を変更する。
•
join(int time): waits for the indicated milliseconds before commanding the thread to die英語のヒント
• join(int time):指定されたミリ秒数だけ待った後で、スレッドに終了を命じる。
CONCEPTS IN PRACTICE概念を実践する
Which HLL Works Best?どの高水準言語が最も適しているか
Modern HLLs combine a variety of programming models to make it easier to tackle problems in various domains and industries. Two of the models we have studied are object-oriented programming and concurrency.
英語のヒント
現代の高水準言語は、さまざまな分野や産業の問題に取り組みやすくするため、多様なプログラミングモデルを組み合わせている。これまでに学んだモデルのうち二つが、オブジェクト指向プログラミングと並行性である。
The need for an object-oriented model may be dictated by the need for a high degree of abstraction capability. Other reasons include improved productivity in software development, cost, scalability, and maintainability.
英語のヒント
高度な抽象化能力が必要なために、オブジェクト指向モデルが必要になる場合がある。ほかの理由には、ソフトウェア開発の生産性の向上、費用、拡張性、保守性がある。
OOP also adapts well to parallelism, which is another paradigm to consider. A need for speed can require the selection of an HLL that supports concurrency and/or parallelism well. It may be the case that the application we build requires both.
英語のヒント
OOPは、検討すべきもう一つのパラダイムである並列処理にも適している。速度を求める場合、並行性や並列性を十分に支える高水準言語を選ぶ必要があるかもしれない。作成するアプリケーションが、両方を必要とする場合もある。
We have choices, the most popular of which are C++, Java, and C#. Narrowing down our choices is helpful and puts us in a great position to examine each to make a well-informed decision.
英語のヒント
選択肢は複数あり、特に人気があるのはC++、Java、C#である。候補を絞り込むと、一つずつ調べ、十分な情報に基づく判断を下しやすくなる。
Sometimes OOP is desirable, but concurrency may not be. This would occur when programming operating systems such as Windows with C++. It has an object-oriented UI, but it must also manipulate the machine elements directly. In this case concurrency may not be desirable, so some modules should be programmed without concurrency by coding them in just the C aspects of the language.
英語のヒント
OOPが望ましくても、並行性は望ましくない場合がある。C++でWindowsのようなオペレーティングシステムをプログラムする場合などがそうである。オブジェクト指向のユーザーインターフェースを持つ一方、機械の要素を直接操作する必要もある。この場合には並行性が望ましくないことがあるので、一部のモジュールは、その言語のCの部分だけでコードを書き、並行性を使わずにプログラムすべきである。