/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/@Overridepublicvoidrun(){if(target != null){
target.run();}}
3.2 基础练习
//继承thread使用启动一个线程实现每1s输出一句话,第10句话结束publicclassTest1{publicstaticvoidmain(String[] args){//继承thread使用//启动一个线程实现每1s输出一句话,第10句话结束newTalk().start();}}//创建一个Talk类继承Thread实现每隔一秒输出一句话classTalkextendsThread{int index =1;@Overridepublicvoidrun(){do{
System.out.println("这是第"+ index +"句话");//暂停一秒try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}++index;}while(index !=11);}}
3.3 线程run和start的区别
如果run的话仅仅只是默认执行的是run方法并不会重新启动一个线程,而是在main主线程中串行执行
3.3.1 线程run
/**
* @author luqianqi
* @title: Test
* @projectName thread
* @description: TODO
* @date 2021/5/99:59 上午
*/publicclassTest1{publicstaticvoidmain(String[] args){//继承thread使用//启动一个线程实现每1s输出一句话,第10句话结束newTalk().run();//当主(main)线程启动一个子线程(talk)时,主不会阻塞等待子线程结束,而是会继续下进行for(int i =1; i <=10; i++){
System.out.print("main方法的线程名:"+Thread.currentThread().getName());
System.out.println("主线程i = "+ i);try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}}}}//创建一个Talk类继承Thread实现每隔一秒输出一句话classTalkextendsThread{int index =1;@Overridepublicvoidrun(){do{
System.out.println("这是第"+ index +"句话");
System.out.println("当前线程名:"+Thread.currentThread().getName());//暂停一秒try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}++index;}while(index !=11);}}
3.3.2 线程start
线程start才是会真正的重新启动一个新线程,执行我们的业务代码,并且主线程不会阻塞继续往下执行
/**
* @author luqianqi
* @title: Test
* @projectName thread
* @description: TODO
* @date 2021/5/99:59 上午
*/publicclassTest1{publicstaticvoidmain(String[] args){//继承thread使用//启动一个线程实现每1s输出一句话,第10句话结束newTalk().start();//当主(main)线程启动一个子线程(talk)时,主不会阻塞等待子线程结束,而是会继续下进行for(int i =1; i <=10; i++){
System.out.print("main方法的线程名:"+Thread.currentThread().getName());
System.out.println("主线程i = "+ i);try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}}}}//创建一个Talk类继承Thread实现每隔一秒输出一句话classTalkextendsThread{int index =1;@Overridepublicvoidrun(){do{
System.out.println("这是第"+ index +"句话");
System.out.println("当前线程名:"+Thread.currentThread().getName());//暂停一秒try{
Thread.sleep(1000);}catch(InterruptedException e){
e.printStackTrace();}++index;}while(index !=11);}}
publicsynchronizedvoidstart(){/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/if(threadStatus !=0)thrownewIllegalThreadStateException();/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);boolean started =false;try{start0();
started =true;}finally{try{if(!started){
group.threadStartFailed(this);}}catch(Throwable ignore){/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */}}}privatenativevoidstart0();//native方法是jvm虚拟机调用的方法,start0以后不会立即执行而是将线程变成可执行状态,由cpu执行统一调度