java--Threads believe that when you learn Java, you are expected to have a deep impression on threads, and now when I start to touch Android development, I really find out how important threads are, and now I share my understanding of threads. We must distinguish between threads and processes is not the same thing, what is the process? The process is as if we need to execute the class file, and the thread is actually invoking the CPU resources to run. A class file typically has only one process, but threads can have many, and a thread executes asynchronously when executed. No more nonsense, straight to the subject. First, how to open a thread in the main function: Public classThread_one { Public Static voidmain (String [] args) {Run Run=NewRun (); //Run.run ();//This is a method call, and there's a difference between threadsThread thread =NewThread (run); Thread.Start ();//start thread, call thread's Run () method for(intI=1; i<=20; i++) {System.out.println ("Main thread I value:--------" +i); } }}classRunImplementsrunnable{@Override Public voidrun () { for(intI=1; i<=20; i++) {System.out.println ("The value of the child thread I:" +i); }}} Two, the sleep method in the thread Public classThread_sleep {/** Thread pauses*/ Public Static voidmain (String [] args) {Runone run=NewRunone (); Thread Thread=NewThread (run); Thread.Start (); Try{Thread.Sleep (5000); Thread.Interrupt ();//execution of interrupt thread in//thread.stop ();//relative to the thread break, stop is too rough, it is not recommended, generally use this method when you need to force a child thread to close}Catch(interruptedexception e) {e.printstacktrace (); } }}classRunoneImplementsrunnable{@Override Public voidrun () { for(intI=1; i<10; i++){ Try{Thread.Sleep (1000); System.out.println ("-----"+NewDate () + "-----"); } Catch(interruptedexception e) {return;//Close child threads directly when capturing to a child thread is interrupted}}} Here is a special note, Thread.Interrupt (); Can be in the execution of thread break, relative to stop gentle so a little bit, but not the best way to close the thread, the following will provide you with a Type of method: Public classThread_stop { Public Static voidmain (String [] args) {Runthree run=NewRunthree (); Thread th=NewThread (run); Th.start (); Try{Thread.Sleep (5000); } Catch(interruptedexception e) {e.printstacktrace (); } run.setstop (); }}classRunthreeImplementsrunnable{BooleanFlag; @Override Public voidrun () {flag=true; inti = 0; while(flag) {Try{System.out.println ("Child thread----" + (i++)); Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } } } Public voidSetstop () {flag=false; The following is a brief introduction to the merge and yield in threads: first, how to merge threads, where the Join () method is called Public classThread_join {/** Merge Threads*/ Public Static voidmain (String [] args) {runtwo run=NewRuntwo (); Thread Thread=NewThread (run); Thread.Start (); Try{thread.join ();//merge thread, which is equivalent to a method call}Catch(interruptedexception e) {e.printstacktrace (); } for(inti=0; i<10; i++) {System.out.println ("Main thread:" +i); } }}classRuntwoImplementsrunnable{@Override Public voidrun () { for(inti=0; i<10; i++) {System.out.println ("Child Thread:----" +i); }}} II, how to let the thread, here called the yield () method of the Threads: Public classThread_yield {/**Give up the CPU *@paramargs*/ Public Static voidMain (string[] args) {th th=NewTh ("AAA"); Th.start (); for(inti = 0; i<=10; i++) {System.out.println ("Main thread----" +i); } }}classThextendsthread{th () {} th (String s) {Super(s);} @Override Public voidrun () { for(inti = 0; i<=10; i++){ if(i%3!=0) {System.out.println ("Child Threads" +i); }Else{System.out.println ("Child thread i=" +i+ "thread to toggle"); Yield ();//This method can be used by inheriting from threadFinally, let's share with you some questions about the priority of the thread: Public classthread_priority {/** Priority set thread priorities * Thread default priority is 5;thread with a maximum priority of 10 and a minimum of 0*/ Public Static voidmain (String [] args) {T1 T1=NewT1 (); T2 T2=NewT2 (); T1.start (); //t1.setpriority (thread.norm_priority+3);//set the priority of T1T2.start (); }}classT1extendsthread{@Override Public voidrun () { for(inti = 0; i<50; i++) {System.out.println ("Thread T1-----" +i); } }}classT2extendsthread{@Override Public voidrun () { for(inti = 0; i<50; i++) {System.out.println ("Thread T2" +i); }} I believe that the above code has basically already understood the threading mechanism in Java, for thread synchronization, I will communicate with you in the next article, if you have any questions welcome harassment. Finally, the above content if there are errors, but also hope pointing.
Http://www.cnblogs.com/AndroidJotting/p/3936292.html
java--Threads