Vegetable rookie Diary multi-Threading ()

Source: Internet
Author: User

Date: October 28, 2015

Location: XXXX

XXXXXXX

My name is xxx, I am a rookie programmer, is trying to become a qualified programmer on the road to study forward. But this is the mask of my identity on the surface, in fact, I am an actor, not wrong (recently in order to learn more about human beings, see too much of a very confidential information called the film, so a bit conditioned reflex), in fact, I am an alien, n years ago, my star was to invade this small planet called the Earth. But cautious we sent a predecessor of mine to come here to understand the information, fortunately, otherwise we will be the whole army collapsed, too risky. This is a magical place, in which the predecessor of the report, here casually a person can be in some inexplicable situation down to somewhere, even with our technology also do not know where it is, to know that we are a large empire across several universes, they call this a cross. There is a casual looking luotuo (not just the grandfather!!!) May be a peerless master, they seem to love the sipilailian of their own skills to some of the not-so-good and love to kill the family's nickname called Dick Silk Group. Those who will kill the quartet, waved to destroy a planet, too cruel. To know that even our cosmic combat units, if the main cannon has the ability to destroy the planet, and the planet seems to have been destroyed countless times, but will be a group called heroes to restore, they have a kind of beads (such as dragon beads or something), some with the fairy law or something, too magical. There is a place called R country or the home of the cosmic monsters, often have monsters back, but all disappeared ... And a seemingly ordinary person could be this or that God's illegitimate child or something like that. The gods here are said to be omnipotent. There are many but my level is not enough, the old man brought back the important news know not much, in short, the earth is too dangerous, I often want to go home xxx star.

But in order to penetrate the enemy inside, I will stick to the end. Well, I heard that the predecessor is in a place called XX Internet cafes (what to do with it), with two boxes we are not much use of Kingada, which seems to be called gold in a What XX website of a very mysterious database to see. I have also been there, but according to nearby Earth people say which has been demolished (demolition is a what?) , it must have been hidden by the enemy. But it's okay, I'll try to find more useful information.

At the XXX address There are reports of Tell's creeps.

The Star Creeps report: report general, I found a secret!!!!

You can see the above novel and the following text this is the multi-threaded two programs, you are cup you want to see which one to see that.

If you want to read the next time the novel is a single thread. Must read this to know the address to see the next. (I have no way of judging others to see the final tell, otherwise it's like)

What is multithreading: a technique that implements concurrent execution of multiple threads from software or hardware.

So what is a thread?

           What is a thread: Each program that is running on the system is a process. Each process contains one or more threads. A process can also be a dynamic execution of an entire program or part of a program. A thread is a set of instructions , or a Special section of a program that can be executed independently in a program . It can also be understood as the context in which the code runs . So a thread is basically a lightweight process that is responsible for multitasking in a single program . Typically, the operating system is responsible for scheduling and executing multiple threads. (Baidu Encyclopedia)

a thread, sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a register collection, and a stack . In addition, the thread is an entity in the process, is the basic unit that is dispatched and dispatched by the system independently, the thread does not own the system resources, only has a bit of resources that are essential in the operation, but it can share all the resources owned by the process with other threads belonging to one process. One thread can create and revoke another thread, which can be executed concurrently between multiple threads in the same process. Because of the mutual constraints between threads, the thread is running in a discontinuous. Threads also have three basic states of readiness , blocking , and running. A ready state is a thread that has all of the conditions running, is logically capable of running, is waiting on a processing machine, and a running state is a thread-owning processor that is running; a blocking state is a thread waiting for an event, such as a semaphore, to be logically unenforceable. Each program has at least one thread, and if the program has only one thread, it is the program itself.

A thread is a single sequential control flow in a program. A relatively independent, scheduled execution unit within a process is the unit of dispatch for the system to dispatch and dispatch CPUs independently of the operating program. Run multiple threads at the same time in a single program to do different work, called multithreading (good search)

I personally understand: We write the program is running in the process itself, the process itself is an environment, it is actually performing the operation of the procedure is a thread if your program to perform more than one operation, the single-threaded word will be executed sequentially. For example, arithmetic plus, subtraction (not a resource, just two operations), such as writing is to perform addition after subtraction. The program will perform the addition and then perform the subtraction, the addition of some problems will not exit but will be stuck on the card for 1 hours, subtraction is not executed, if the subtraction only in less than a second to finish it? If it is multi-threaded, you can have a thread to perform the addition, a thread to perform the subtraction, they will be executed separately, the addition card, the first to perform subtraction, in addition, at least the task can be completed first (do not think fast? )。

I have a limited understanding of the thread, it is not swim, say something I know it is very obvious.

How to use multithreading in Java:

There are two ways: an implementation interface runnable

The runnable interface has only one run method, the function is: (personal understanding) is the new thread's main method, the thread began to automatically execute the method (but to start the thread to use the St Art method, only the following thread class has this method, which he does not own)

Method:

A class to turn on threading

 Public classTestImplementsRunnable {@Override Public voidrun () {//for testing       for(inti = 0; I < 10; i++) {System.out.println (i+ "\ T"); if(i==2) {            Try {
Sleep state Thread.Sleep (100000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } }}

Open a thread

     Public Static void Main (string[] args) {        /         /First //Runnable test1=new Test ()  ;        Test test1=new  test ();         // the thread class has an object that constructs a method that can pass in an runnable interface        Thread th=new  thread (test1);                Th.start ();

//A native method is a Java interface that calls non-Java code

one is to inherit the thread class, which implements the Runnable interface

The method is:

 Public classTest2extends Thread{@Override Public voidrun () {//for testing           for(inti = 0; I < 10; i++) {System.out.println (i+ "\ T"); if(i==2) {                Try {
Thread.Sleep (100000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } }}

Open thread in Main method

            // a second        Test2 test2=New  Test2 ();        Test2.start ();

In fact, it is the thread class that actually implements invoking the underlying code to finish opening the thread. Runnable interface is to avoid the disadvantages of single inheritance in Java and the existence of resource sharing (personal understanding, not guaranteed)

Status of the thread: new state, ready state, running state, blocking state, and death status.

Referenced from: http://blog.csdn.net/peter_teng/article/details/10197785

/*
1. New status (new):
When a thread is created with the new operator, such as new Thread (r), the thread has not started running, and the thread is in the new state. When a thread is in a new state, the program has not started running code in the thread

2. Ready state (Runnable)

A newly created thread does not automatically start running, and the thread's start () method must be called to execute the thread. When the thread object calls the start () method, the thread is started, thestart () method creates the system resources that the thread runs, and the dispatch thread runs the run () method. When the start () method returns, the thread is in the ready state.

A thread that is in a ready state does not necessarily run the run () method immediately, and the thread must also compete with other threads for CPU time, only to get CPU time to run the thread. Because in a single CPU computer system, it is not possible to run multiple threads at the same time, only one thread is running at a time. Therefore, there may be multiple threads in the ready state at this time. Multiple threads that are in a ready state are dispatched by the thread scheduler (ThreadsScheduler) of the Java Runtime System.

3. Operating status (Running)

When the thread gets CPU time, it goes into a running state and really starts executing the run () method.

4. Blocking status (Blocked)

During a thread run, it may enter a blocking state for various reasons:
1> thread goes to sleep by calling the sleep method;
The 2> thread invokes an operation that is blocked on I/O, that is, the operation does not return to its caller until the input-output operation is complete;
The 3> thread attempts to get a lock that is being held by another thread;
4> thread is waiting for a trigger condition;
......

The so-called blocking state is that the running thread is not running at the end, temporarily giving up the CPU, while other in the ready state of the thread can get CPU time, into the running state.

5. Death status (Dead)

There are two causes of thread death:
1) The Run method normally exits and dies naturally,
2) An uncaught exception terminates the Run method and causes the thread to sudden death.
To determine whether a thread is currently alive (or is either operational or blocked), the IsAlive method needs to be used. This method returns true if it is operational or blocked, or false if the thread is still in the new state and is not operational, or the thread is dead.

*/

Vegetable rookie Diary multi-Threading ()

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.