The first method thread open: by creating an object of a subclass of Thread way

 
cn.itcast.demo16.demo06.Thread Package ; 

/ **
* @author newcityman
* @date 2019/7/22 - 21:47
* /
public class the MyThread the extends the Thread {

public void RUN () {
String name = getName () ;
the System. Out.println ( "current thread name:" + name) ;
for ( int I = 0 ; I < 20 is ; I ++) {
. the System Out.println ( "thread" + I) ;
}
String NAME1 = the thread. . currentThread () getName () ;
. System (out.println "The second way to obtain the thread's name:" + NAME1) ;
}
}


Package Penalty for cn.itcast.demo16.demo06.Thread;

/ **
*@authornewcityman
*@date2019/7/22 - 21:51
The first way to create multi-threaded *:
* java.lang.Thread categories: is a description of the thread class, we want to achieve a multi-threaded program, it is necessary to inherit the thread class
*
* Implementation steps:
* 1, create a subclass of the thread class
* 2, overridden in a subclass of the thread class thread class run method, set the threaded tasks (open thread What to do?)
* 3, to create an object of a subclass of the thread class
* 4, call the start () method of the thread class sub-class object, open a new thread run method
* void start () Causes this thread to begin execution; java virtual machine calls the run method of this thread;
* result is that two threads run concurrently; the current thread (main thread) and another thread (new thread is created, which executes its run method)
* repeatedly to start a thread is illegal. Special equipment is when the thread has completed execution, can not be restarted.
* Java program is part of preemptive scheduling, the high priority thread, the thread execution priority; the same priority, a random selection execution
* /
public class Demo01Thread {
static void public main (String [] args) {
// create a subclass of class Thread object
the MyThread MT = new new the MyThread () ;
mt.start () ;

for ( int I = 0 ; I < 20 is ; I ++) {
the System . Out.println ( "main" + I) ;
}
}
}

Guess you like

Origin www.cnblogs.com/newcityboy/p/11229013.html