[Learn Notes] Thinking in Java (The 2nd Edition) Study Note (4)

xiaoxiao2021-03-05  21

Chapter 13 Creating Windows and SMDs

slightly

Chapter 14 Multithreading

Multi-thread implementation can have two ways: inheriting and implementing Runnable interfaces from Thread class

1. Inheriting from threads To create a thread, the easiest way is to inherit from Thread class. This class contains everything you need to create and run the thread. The most important method of Thread is Run (). However, in order to use Run (), it must be overloaded or covered to make it fully active. Therefore, Run () belongs to the code that will "concurrency" or "simultaneously" in the program. Although it is heavy as Run (), but when running the thread, it is necessary to call the START () method; see the example below:

public class SimpleThread extends Thread {private int countDown = 10000; private int threadNumber; private static int threadCount = 0; public SimpleThread () {threadNumber = threadCount; System.out.println ( "Making" threadNumber);} public void Run () {while (true) {system.out.println ("Thread" THREADNUMBER "(" countdown ")"); if (- countdown == 0) Return;}} public static void main String [] args) {for (int i = 0; i <5; i ) new simplethread (). Start (); system.out.println ("all threads started");}}

The Run () method is almost certainly containing some form of loop - they will continue until the thread is no longer needed. Therefore, we must specify specific conditions to interrupt and exit this cycle (or in the above example, simply returning from RUN ()). Run () is usually in the form of an infinite loop. That is to say, it will run forever by blocking the STOP () or Destroy () call to the thread, it will always run (until the program is completed).

[Sharing limited resources] can imagine a single-threaded program into an isolated entity, which can traverse our problem space, and only one thing can be done once. Since there is only one entity, I will never worry that there will be two entities at the same time trying to use the same resources, just like two people want to stop a parking space, and they want to pass a door, even simultaneously. After entering the multi-threaded environment, they are never isolated. There may be two or more threads try to simultaneously the same limited resources. This potential resource conflict must be prevented, otherwise two threads may have access to a bank account at the same time, print to the same computer, and adjust the same value, etc.

[A thread can have four states] (1) New: thread object has been created, but it has not been started, so it is not possible. (2) Runnable: Means that once the time slider is provided to a thread, the thread can start running immediately. Therefore, threads may be, or may not be running, but once condition license, there is nothing to stop its operation - it has neither "dead" and is not "blocked". (3) Dead: After returning from your own Run () method, a thread has been "dead". You can also call the stop () to die, but it will generate a violation - a subclass belonging to Error (that is, we usually do not capture it). Remember that a violation of "throw" should be a special event instead of the normal program run. So don't recommend you to use STOP () (in Java1.2 is resolutely opposed). There is also a destroy () method (which will never be implemented), avoid calling it as much as possible, because it is very arbitrary, and does not unlock the object at all. (4) Block: The thread can run, but there is something that hinders it. If the thread is in a plug, the scheduling mechanism can simply skip it and do not assign any CPU time. No operation is not taken unless the thread enters the "run" status again. [Why is it blocked】 The blockage is the most interesting in the four states, it is worthy of further discussion. The thread is blocked may be caused by the following five aspects: (1) Call the SLEP (Mr.) so that the thread enters the "sleep" state. This thread will not run in the specified time. (2) Suspend the execution of the thread with suspend (). Unless the thread receives the resume () message, the "run" state is not returned. (3) Suspend the execution of the thread with Wait (). Unless the thread receives the Nofify () or NotifyAll () message, it will not become "run" (yes, this looks very similar to the reason 2, but there is an obvious difference that we will reveal immediately). (4) The thread is waiting for some IO (input and output) operations. (5) The thread attempts to call another object's "synchronization" method, but that object is in the locked state, temporarily unwilling.

2. Implement Runnable interface

Chapter 15 Network Programming 1. The machine identification IP exists in two forms: (1) The most familiar DNS (Domain Name Service) form (2) is separated by the order number (.), Such as 202.98.32.1112. The most basic spirit of the network is to let two machines connect together and "talk" or "communication". 3. When we set up a client or server, you must choose a port that is recognized regardless of the client or the server. Just as we went to meet someone, the IP address is his house, while the port is the room he is. 4. Sockets "socket" or "socket" is also an abstraction of software for expressing one of the two machines connected to "terminal". For a specific connection, there is a "socket" on each machine, you can imagine there is a virtual "cable" between them. Each end of the cable is inserted into a "socket" or "socket". Of course, physical hardware and cable connections between machines are completely unknown. Abstract basic purpose is to let us do not know how detail as much as possible.

Chapter 16 Design Mode Paradigm Category "Design Patterns" A book discusses 23 different paradigms, and based on three standard classifications (all standards involve aspects that may change). These three standards are: (1) Creating: Object creation methods. This usually involves the isolation of the object creation details, so it does not have to rely on the specific type of object, so it does not have to change the code when adding an object type. (2) Structure: Design objects to meet specific project restrictions. This involves the connection of objects and other objects to ensure that changes within the system do not affect these connections. (3) Behavior: Objects for manipulating a specific type of action in the program. This requires that we will be able to take the operation package, such as explaining a language, implementing a request, traversing in a sequence (like it is in the inner) or implemented an algorithm. This chapter provides examples of "Observer" and "Visitor" paradigm. Chapter 17 Project // 2005-4-5

-------------------------------------------------- ----------------

I can finally take a paragraph, I spent more than 3 months before and after, I have a little harvest, but more important is the lack of opportunities for practice; next step? What should I do next?

转载请注明原文地址:https://www.9cbs.com/read-38125.html

New Post(0)