[Learn notes] Thinking in Java (the 2nd edition) Study (4)
Source: Internet
Author: User
Notes
Chapter 13th creating Windows and Programs
Slightly
14th Chapter Multithreading
There are basically 2 ways to implement Multithreading: Inherit from the thread class and implement the Runnable interface
1. The easiest way to inherit from a thread to create a thread is to inherit from the thread class. This class contains everything you need to create and run a thread. The most important method of thread is run (). However, in order to use Run (), it must be overloaded or overwritten so that it can act in full accordance with its own instructions. Therefore, run () belongs to code that will execute "concurrent" or "simultaneous" with other threads in the program. Although the overload is run (), the start () method needs to be invoked when running the thread, as shown in the following example:
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 +) "); & nbsp; if (--countdown = 0) return; } } public static void Main (string[] args) { for (int i = 0; i < 5; i++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; new Simplethread (). Start (); System.out.println ("All Threads Started"); }}
The run () method almost certainly contains some form of looping-they continue until the thread is no longer needed. Therefore, we must specify conditions to break and exit the loop (or, in the example above, simply return from Run ()). Run () usually takes the form of an infinite loop. That is, it will run forever (until the program completes) by blocking the external issue of a stop () or destroy () call to the thread.
"Sharing limited resources" can be thought of as an isolated entity that can traverse our problem space and only do one thing at a time. Because there is only one entity, you never have to worry about two entities trying to use the same resources at the same time, as two people want to stop at the same time and want to go through a door, even at the same time. After entering a multithreaded environment, they are no longer isolated. There may be two or more threads trying to be at the same time with a limited resource. This potential resource conflict must be prevented, or two threads may be able to access a bank account at the same time, print to the same computer, adjust the same value, and so on.
"One thread can have four states" (1) NEW: The Thread object has been created but has not been started, so it cannot be run. (2) Operational (Runnable): means that once the time slicing mechanism has idle CPU cycles available to a thread, that thread can start running immediately. As a result, the thread may or may not be running, but once the conditions permit, nothing can prevent it from running-it is neither "dead" nor "blocked". (3) Death (Dead): After returning from its own run () method, a thread is "dead". You can also call Stop () to die, but it will create an offence--a subclass of the error (that is, we usually don't capture it). Remember that a "toss" of a violation should be a special event, not part of the normal program's operation. Therefore, it is not recommended that you use Stop () (in Java1.2 is resolutely opposed). There is also a destroy () method (which is never implemented) and should avoid calling it as much as possible because it is so arbitrary that it does not unlock the object at all. (4) Blockage (Blocked): A thread can run, but there is something blocking it. If the thread is in a blocked state, the scheduling mechanism can simply skip it and not allocate any CPU time to it. No action is taken unless the thread enters the "run" state again.
"Why Jam" is the most interesting of the four States mentioned above, which is worth further discussion. Thread blockage can be caused by the following five reasons: (1) call sleep (milliseconds) to make the thread into a "sleeping" state. This thread is not going to run within the specified time. (2) suspend the execution of the thread with suspend (). The run state is not returned unless the thread receives the resume () message. (3) suspend the execution of the thread with Wait (). Unless the thread receives a nofify () or notifyall () message, it will not become "operational" (Yes, it looks very similar to reason 2, but there is one obvious difference that we are going to reveal soon). (4) The thread is waiting for some IO (input/output) operation to complete. (5) The thread tried to invoke the "sync" method of another object, but the object was locked and temporarily unavailable.
2. Implement Runnable interface
15th Chapter Network Programming 1. The machine identity IP exists in two ways: (1) The most familiar form of DNS (Domain Name Service) (2) is divided by the point number (.) of four sets of numbers, such as 202.98.32.1112. The basic spirit of the network is to connect the two machines together and "talk" or "communicate" with each other. 3. Port when we set up a client or server, we must select a port that both the client and the server recognize the connection. Just like when we visit someone, the IP address is the house he lives in, and the port is the room where he is. 4. Socket "Socket" or "socket" (socket) is also a form of software abstraction, used to express the two machines a connection between the "terminal." There is a "socket" on each machine for a particular connection, and you can imagine a virtual "cable" between them. Each end of the cable is inserted into a "socket" or "socket". Of course, the physical hardware and cable connections between machines are completely unknown. The basic tenet of <b> abstraction is that we don't have to know the details </b> as much as possible.
The 16th chapter Design Pattern Paradigm Classification The book "Designing Patterns" discusses 23 different paradigms and classifies them according to three criteria (all of which are related to aspects that may change). The three criteria are: (1) Create: How objects are created. This usually involves the isolation of object creation details so that you do not have to rely on specific types of objects, so you do not have to change the code when adding an object type. (2) Structure: Design objects to meet specific project constraints. This involves how objects are connected to other objects to ensure that changes within the system do not affect those connections. (3) Behavior: An object that manipulates a particular type of action in a program. This requires that we encapsulate the actions we want to take, such as interpreting a language, implementing a request, traversing in a sequence (as in a successor), or implementing an algorithm. This chapter provides examples of the paradigm of the Observer (Observer) and accessors (Visitor).
Finally can end up, after spending 3 months to finish, some Xu harvest, but more important is lack of practice opportunities; next? What's the next step?
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service