Thread 1-Thread common methods

Source: Internet
Author: User

1. Threading method
1), Start (), start the thread, and go to the ready state, ready for execution
2), run (), overriding method, starting execution thread
3), CurrentThread (), calling the current thread
4), GetName (), get the thread name
5), SetName (), set the thread name
6), yield (), call this method of the thread, release the current CPU execution, CPU execution other threads, object lock will not release
7), join (), call the Join method of the B thread in a thread, a thread pauses execution, executes the b thread until the B thread finishes executing,
A thread continues execution after the join () method is followed by the code.
8), isAlive (), whether the thread survives
9), Sleep (l), so that the current thread sleeps l milliseconds.
10), Wait (), notify (), Notifyall ()

 Packagecom.thread.test;classThread1extendsthread{@Override Public voidrun () {thread.currentthread ();        System.out.println (Thread.CurrentThread (). GetName ());  for(inti = 1; I <=100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"===="+i); }    }}/*** Thread Main method * 1, start (), start thread, and go into ready state, wait for execution * 2, run (), override method, start thread * 3, CurrentThread (), call current thread * 4, GetName () , get the thread name * 5, SetName (), set the thread name * 6, yield (), call this method of the thread, release the current CPU execution, the CPU executes other threads, the object lock will not release * 7, join (), call the B thread's join method in a thread, a thread Pause execution, execute the b thread until the B thread finishes executing, and the code after the join () method continues execution. * 8, IsAlive (), whether the thread survives * 9, sleep (l), so that the current thread sleeps l milliseconds. */ Public classtestthread{ Public Static voidMain (string[] args) {thread.currentthread (). SetName ("Main Thread"); Thread1 T1=NewThread1 (); T1.setname ("Sub-thread 1");                T1.start ();  for(inti = 1; i<= 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+"......"+i); if(i% 10 = = 0) {thread.currentthread (); Thread.yield ();//Release Current CPU            }            if(i = = 20) {Thread1 t2=NewThread1 (); T2.setname ("Sub-thread 2"); Try{t2.join (); } Catch(interruptedexception e) {e.printstacktrace (); }            }        }    }}

2. Thread Priority
Thread scheduling
(1) Time slice strategy, with priority thread, composed of FIFO queue, using time slice strategy
(2) Preemptive strategy, high priority first execution

Thread 1-Thread common methods

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.