线程的run()方法带参情况
生活随笔
收集整理的這篇文章主要介紹了
线程的run()方法带参情况
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先看一下Runnable接口的源碼,整個方法體里就只有一個run的抽象方法,所以才創(chuàng)建實現類實現該接口的時候是肯定要重寫接口內的抽象方法的(也就是run()方法),按照重寫規(guī)則(不能是抽象,參數要和父類一致,也就是無參)重寫run()方法,方法體就可以自定義添加了。
@FunctionalInterface public interface Runnable {/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.** @see java.lang.Thread#run()*/public abstract void run(); }但是我在百度上找到了這么一個題
能看到run()方法的參數是Thread t,并不是無參但是能夠成功運行,以下是run()方法可以帶參的情況:
要實現帶參數的run()方法:
示例:
class MyThread extends Thread{int a = 0;//定義線程內變量public MyThread(int a){//定義帶參數的構造函數,達到初始化線程內變量的值this.a=a;}@Overridepublic void run() {this.a++;//線程內變量自增} }引用一位網友的回答補充說明
總結
以上是生活随笔為你收集整理的线程的run()方法带参情况的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DC基本知识问答
- 下一篇: DOS命令行数据乱码解决