プリンキピア

第7章高水準プログラミング言語

Polymorphismポリモーフィズム
The feature of OOP in which methods that are inherited may perform in different ways in different subclasses depending on their context is called polymorphism. This is implemented overriding inherited methods in a subclass or the ones in an implemented interface. In the UML diagram, the launch behavior of a car is very different from that of a sailboat or a rocket ship. The resulting behavior is dependent on the context because it is not set at compile time. Rather, the runtime decides what to do when it encounters an object based on its class. The behavior morphs and produces different results for the same method calls depending on the object in hand, which is dynamic method binding or late binding, meaning that all methods are resolved dynamically at runtime, not by the compiler.
英語のヒント
OOPでは、継承したメソッドが、文脈に応じて、異なるサブクラスで異なる方法で動作することがある。この特徴をポリモーフィズムという。これは、サブクラスで継承したメソッドや、実装したインターフェース内のメソッドをオーバーライドすることで実現する。UML図では、車のlaunchという振る舞いは、帆船やロケットのそれとは大きく異なる。結果として生じる振る舞いは、コンパイル時には定まらず、文脈によって決まる。実行環境は、オブジェクトに出会ったとき、そのクラスに基づいて何をするかを決める。扱っているオブジェクトによって振る舞いが変化し、同じメソッド呼び出しでも異なる結果を生む。これを、動的メソッド束縛、または遅延束縛という。つまり、すべてのメソッドは、コンパイラーによってではなく、実行時に動的に解決される。
OOP AssessmentOOPの評価
Remember that some languages like Java and C# are completely object-oriented; some like C++ are hybrids of procedural and object-oriented, and some are on the path toward complete object-oriented like JavaScript, PHP, and Python. Any assessment of the object-oriented paradigm must be taken in the context of where a particular language is on the scale of object orientation.
英語のヒント
JavaやC#のように完全にオブジェクト指向の言語もあれば、C++のように手続き型とオブジェクト指向を混ぜた言語もあり、JavaScript、PHP、Pythonのように完全なオブジェクト指向へ向かう途中にある言語もあることを思い出そう。オブジェクト指向パラダイムを評価する際は、その言語がオブジェクト指向の度合いを表す尺度のどこに位置するかを踏まえる必要がある。
OOP advantages:
OOPの利点:
• Strong abstraction with the ability to reuse code efficiently
英語のヒント
• 強力な抽象化と、コードを効率的に再利用する能力。
• Improved software development productivity and maintainability
英語のヒント
• ソフトウェア開発の生産性と保守性の向上。
• Faster software development, thus lower cost of development as it is very efficient for parallel development
英語のヒント
• 並行して開発を進めるのに非常に効率的なので、ソフトウェア開発が速まり、結果として開発費が下がる。
• Adapts well to parallelism, where programs can have more than one part of the code running simultaneously
英語のヒント
• プログラム内の複数の部分を同時に実行できる、並列処理に適している。
• More consistent software—dynamic method binding producing polymorphism are a mandate that subclasses implement the same behaviors with differences
英語のヒント
• より一貫したソフトウェアになる。ポリモーフィズムを生む動的メソッド束縛により、サブクラスは、違いを持たせながらも同じ振る舞いを実装する必要がある。
OOP disadvantages:
OOPの欠点:
• Steep learning curve to master OOP
英語のヒント
• OOPを習得するまでの学習負担が大きい。
• Program creation can be complex
• プログラムの作成が複雑になることがある。
• Slower execution due to more generated instructions by the compiler
英語のヒント
• コンパイラーが生成する命令が多くなるため、実行が遅くなる。

Concurrency and Parallel Programming並行性と並列プログラミング

A process, or thread, is an active execution context, meaning that it is an executable code block. The ability of an application to multitask is called concurrency. When an application can multitask, it can process more than one task at the same time. It is the case of their executions overlapping in time. In concurrency we have the illusion of simultaneous execution. A single core CPU can only run one task at a time while the processor rapidly switches between concurrent processes, thus creating the illusion. We refer to the executable unit as a thread of control because the processor is controlling which thread is executing at a designated time.
英語のヒント
プロセス、またはスレッドは、活動中の実行コンテキスト、つまり実行可能なコードブロックである。アプリケーションがマルチタスクを行える能力を、並行性という。マルチタスクが可能なアプリケーションは、同じ時間帯に複数のタスクを処理できる。それらの実行時間が重なっている状態である。並行処理では、同時に実行されているように見える。シングルコアCPUが一度に実行できるタスクは一つだけだが、プロセッサーが並行するプロセスを高速に切り替えることで、そうした見かけが生まれる。実行単位を制御スレッドと呼ぶのは、どの時点でどのスレッドを実行するかを、プロセッサーが制御しているためである。
In parallelism, programs can have more than one thread of control running at the same time. This is possible only if we have multiple or multicore processors which allow us to produce simultaneous execution. We must program for both concurrency and parallelism in the same way because the execution logic is the same, only the physical hardware is different. The process of coding for these environments is concurrent programming or parallel programming in which a thread can be thought of as an abstraction of a physical processor.
英語のヒント
並列処理では、プログラムの複数の制御スレッドを同時に実行できる。これが可能なのは、同時実行を行える複数のプロセッサー、またはマルチコアプロセッサーがある場合だけである。実行の論理は同じで、物理的なハードウェアだけが違うため、並行性と並列性には同じ方法でプログラムを作らなければならない。これらの環境に向けてコードを書くことを、並行プログラミングまたは並列プログラミングという。そこでは、スレッドを物理的なプロセッサーの抽象化と考えられる。
Race Conditions競合状態
When the condition of a program and its behaviors are not synchronized, a race condition occurs. Race conditions are all about timing because anomalies can occur when we do not know which process will finish first or which part of the program is currently executing. We can exert control over when a process pauses or ends in the run-until-blocked scenario where some mechanism pauses a thread. This situation is not always a negative one; race conditions can sometimes be positive by allowing multithreading to compete unchecked for processor attention.
英語のヒント
プログラムの状態と振る舞いが同期していないと、競合状態が生じる。どのプロセスが先に終わるか、またはプログラムのどの部分が現在実行されているかが分からないと、異常が生じうるため、競合状態ではタイミングが問題となる。何らかの仕組みがスレッドを一時停止する、ブロックされるまで実行する方式では、プロセスがいつ一時停止し、いつ終了するかを制御できる。この状況は、常に悪いわけではない。複数のスレッドが、制限されずにプロセッサーの処理時間を競い合えるようにすることで、競合状態がよい結果をもたらす場合もある。
In many multitasking situations we want to avoid race conditions and execute a degree of control of execution; in other words, we want to synchronize threads in either the interleaved or parallel situations.
英語のヒント
多くのマルチタスクの状況では、競合状態を避け、実行をある程度制御したい。言い換えると、交互に実行する場合でも並列に実行する場合でも、スレッドを同期させたい。
Synchronization同期
The building of cooperation between threads of execution, or synchronization, is often handled by first ascertaining which segments of code form a critical section. A critical section is a code block that influences the results of concurrency or parallelism. A simple example might be an employee program in which one concurrent function calculates employee pay and another one prints the paycheck. The print function cannot proceed to put the amount on the check until the calculation function returns the amount. Other situations that need the use of critical sections are as follows:
英語のヒント
実行スレッド同士の協調関係を築くこと、すなわち同期は、どのコードの部分がクリティカルセクションになるかを最初に確かめることで行う場合が多い。クリティカルセクションは、並行処理や並列処理の結果に影響するコードブロックである。簡単な例として、並行する一つの関数が従業員の給与を計算し、別の関数が給与小切手を印刷するプログラムが考えられる。計算関数が金額を返すまでは、印刷関数は小切手に金額を記入する処理へ進めない。クリティカルセクションを必要とするほかの状況には、次のようなものがある。
• When multiple threads need access to shared memory or resources and the timing of the access can be critical to the eventual result
英語のヒント
• 複数のスレッドが共有メモリや資源へアクセスする必要があり、そのタイミングが最終的な結果を左右する場合。
• Times when processes need to communicate with each other to proceed, often implemented as message passing between processes
英語のヒント
• プロセスが先へ進むために互いに通信する必要がある場合。多くは、プロセス間のメッセージ受け渡しとして実装される。
• Cases where all concurrent processes must finish before execution of the program proceeds
• プログラムの実行を先へ進める前に、並行するすべてのプロセスが終了しなければならない場合。
• Cases where one or more of the processes need some result from another process
• 一つ以上のプロセスが、別のプロセスからの結果を必要とする場合。
As visible in Figure 7.22, when processes run in a requested order, synchronization is occurring.
英語のヒント
図7.22に見られるように、要求された順序でプロセスが動くとき、同期が行われている。
Figure 7.22

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.22 When multiple threads seek access to a critical section, they need synchronization. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.22 複数のスレッドがクリティカルセクションへアクセスしようとするとき、同期が必要になる。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
Since the goal is to avoid negative race conditions, we do not want to over-synchronize because it lessens the degree of parallelism that we need for performance.
英語のヒント
目的は有害な競合状態を避けることなので、過度な同期は望ましくない。性能のために必要な並列性を低下させるためである。