Friday, January 17, 2003, began to learn more threads in Java today.
The thread is some code that can be parallel, independently executed. Before I edited, I can only do one thing, that is, only one thread. Multi-threaded programming is to divide the program task into multiple parallel sub-tasks, at the same time Running, mutual interference. I have a multi-threaded understanding is from fighting games. The two of the fighting games are implemented by two threads, otherwise how can you have your trick, I sent me Shock wave.
(January 18) suddenly thought of a question, added. Is multi-threaded? Is there much a multitasion we usually say? My understanding is that you can't say this. Simply say, multithreading provides a Multiple thread parallel scheduling inside the process, while multitasking provides a mechanism for running multiple processes within an operating system. The basic principle of multi-task operating system (such as Windows) is this: operating system CPU The time slice is assigned to multiple threads, each thread is completed within the operating system specified (note that the plurality of threads here are different processes). The operating system is constantly switching from a thread to another. Thread execution, so reciprocated, macro seems to be executed together. Since these threads are different from different processes, it seems that there are multiple processes to execute at the same time, so There is a multi-task .WhoOPS, true winding. As above, multi-threaded and multitasking is a significant difference. But think again, do you implement multi-threading in an application not to allocate time slice? Since The principle is the same, so many threads can also be said to be multitasking.
After a Java program starts, there is already a thread running, we can initially establish a thread's actual impression through the following example.
Class testthread {public static void main (string args []) {thread t = thread.currentthread (); T.setName ("this thread is running"); system.out.println ("the running thead:" t) ; {For (int i = 0; i <5; i ) {system.out.println ("Sleep Time" i); thread.sleep (1000); // hang thread, that is, let thread take a break, Do not occupy system resources, therefore other threads can continue. Heads here Thread Default represents main thread}} catch (interruptedException e) {system.out.println ("Thread Has WRONG");}}}
This is just a thread, then how do we implement multiple threads? How do we make the thread to arrange what it wants to do? There are two ways to implement line-building body. The first method is through inheritance constructing thread. There is a THREAD class in Java. There is a function run () in this class, which records the operation of the thread to be completed. It seems like the main function main (), the Run () function is running, the thread ends By inheriting this class, we can define our own thread, tell it what to do in the run function. The following program is inheriting a SimpleThread class, using two threads to output HelloWorld.
Public class twothread {
Public static void main (String args []) {new simplethread ("HelloWorld1"). start (); // Create an instance of two threads, as simple New SimpleThread ("HelloWorld2"). start ();}
} Class SimpleThread Extends Thread {file: // Real content here public simplethread (String Str) {super (str); // super represents the direct parent class of the SimpleThread class, here is thread} file: // We want thread Everything is here public void run () {for (int i = 0; i <10; i ) {system.out.println (i " getname ()); try {sleep ((int) (Math) .random () * 1000);} catch (interruptedException e) {}} system.out.println ("DONE!" getname ());}}
The result of run is that the two threads are alternately display their respective HelloWorld ten times, and the output is mixed, because the two threads are running simultaneously.
The second method is to construct a line building by starting the interface. In any of the class of the startup interface, such as the TWothRead class below, you can implement multiple threads, what I need to do is to add a Run in this class Function. The routine is as follows
class TwoThread implements Runnable {TwoThread () {Thread t1 = Thread.currentThread (); t1.setName ( "The first main thread"); System.out.println ( "The running thead:" t1); Thread t2 = new Thread (this, "THEAD"); // Note that this this is indicating that the new thread is T2 will be determined by the THIS object, which is determined by TWothread's Run function to determine system.out.println. "CREATE ANOTHER THREAD"); t2.start (); // Calling this function will cause thread to start trying the try {system.out.println from the RUN function; thread.sleep (3000); Catch (InterruptedException E) {System.out.println ("First Thread Has Wrong");} System.out.Println ("First Thread Exit");}
Public void run () // Defines the run () function, in this program, T2 this new thread will do what will do {Try {for (int i = 0; i <5; i ) {system.out.println ("Sleep Time for Thread 2:" i); Thread.Sleep (1000);}}} Catch (InterruptedExcect E) {System.out.Println ("Thread Has Wrong");} System.out.println ("SECOND) ");} public static void main (String args []) {new twothread (); // trigger constructor}}
Operating results as follows: The running rhread: Thread [The first main thread, 5, main] creat another threadfirst thread will sleepSleep time for thread 2: 0Sleep time for thread 2: 1Sleep time for thread 2: 2first thread exitSleep time for thread 2 : 3Sleep Time For Thread 2: 4second Thread Exit Does anything. We noticed that many Java programs have an Import statement, as if and c #include and Delphi's uses is very iced. IMPORT is the keyword of Java, responsible Package. The package consists of a set of classes and interfaces. It is a tool for managing large namespaces to avoid name conflicts. Java provides a lot of packages for us, mainly as follows:
Java.applet design Applet class
Java.awt window kit contains classes that produce GUI elements
Java.io file input / output class
Java.langjava language, including: objects, threads, abnormal exits, systems, integers, origins, numbers, characters, etc.
Java.NetSocket class and class related to TCP / IP
Java.util Synchronization class
... The idea of the Java class in the evening. As an object-oriented basic element, the idea of the class is more prominent in Java than C . It is not like C to maintain a compatibility to the process language. The Java program is only class, which is a head-oriented object-oriented, for example, "Hello" is also an object, we can call "hello" .EqualsignoreCase () to determine if it is the same as other strings. We usually The main program, which is seen in C, is also encapsulated in Java, reference to the class.
The basic nature of the class is nothing more than the overload, inheritance and polymorphism in Java. The overload is a member function that creates the name and the parameters. The inheritance is from the ancestral class inheritance variable and member function, is portrait. For example, the polymorphism is reflected in the operation of the program, and the instance variable can appear as an instance of the parent class or an example of the subclass as needed.
These things are written, and the stimuli to the head will be more, remember to be more secure, so that the main examiner asked me what is the object of the object, I actually I haven't answered, really Sigulent.