重拾java之线程1
生活随笔
收集整理的這篇文章主要介紹了
重拾java之线程1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[size=medium] 接觸java也有一段時間了,以前選擇java原因就是java很流行,好就業,那是開始學習java只是簡單學習,沒有更深層的理解how ,why。這也是覺得自己的java技術還是這么菜。現在開始重溫下j2se方面的知識,寫點筆記記錄下
概念
線程,就是你寫的一個簡單的程序,運行的載體的就是線程,幫你完成一件事。我的簡單理解就是,線程就像洗衣機一樣,它的功能幫你完成洗衣服,猶豫你很懶堆了一個月的衣服,而此時要洗,肯定是一個洗衣機搞不定了哦,而你要趕時間要把這個一個衣服用最短的時間洗完,你就用多臺洗衣機來洗(純屬假設,如果你家是賣洗衣機的到可試試),剛才那情形就是傳說的多線程,就是多個任務同時運行,并起運行,就是并發了。
運行
你要用洗衣機洗衣服,前提是你已有臟衣服的存在,放在洗衣機里,然后插上電,打開洗衣開關,洗衣機就開始工作了哦。同樣在使用線程時,你要把你要打算做的事要事先編碼好,然后把它添加到線程的run()方法中。最后調用start()方法,ok! 跟洗衣機一樣工作了。隨著高科技和不斷創新的發展,洗衣機洗衣方式也是多種多樣,我見過兩種,一種是很古老的:就是洗衣和脫水時分開的那種,這種用起來很不方便,需要自己脫水(我很討厭這種洗發)。一種是很先進的:就是洗衣和脫水完美的融為一體,這樣很方便了,不要你親自來脫水(很喜歡這種洗發)。其實實現線程的方式也有兩種了。第一種就是很直接的使用Thread類搞定。繼承Thread 在run()方法中放入你要實現的功能。如下代碼
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("hello ! i'm from Thread");
}
}
在run方法中只是一句簡單的輸出語句。然后運行,代碼如下
public static void main(String [] args){
ThreadDemo demo=new ThreadDemo();
demo.start();
}
好了第一種線程運行方式搞定,接下來看第二種,就是實現Runnable接口了
public class ThreadDemo2 implements Runnable{
@Override
public void run() {
System.out.println("hello ! i'm from Runnable");
}
}
這種方式就是逼著你要實現run()方法了。其執行也略有不同了
public static void main(String [] args){
Thread demo=new Thread(new ThreadDemo2());
demo.start();
}
實現Runnable執行需要一個載體了,為什么了?看Runnable的源碼
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();
}
看起來是不是感覺so easy!它就一個run()方法,就是有裝任務的地方,就是沒發讓功能執行,所以需要求助外援了(純屬簡單理解,寫的不夠嚴謹)。但為什么要選Thread了,這要看源碼了
public class Thread implements Runnable {
............
}
看到沒,Thread 也實現了Runnable,看來他們有父子之情呀,從面向對象世界來說,子原則上都比父功能更強大豐富(現實中父子誰強誰弱這就無比復雜了,還是面向對象世界單純)。
所以子要繼承父的所有功能,看Thread如何實現,上源碼
/**
* Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
* {@code (null, target, gname)}, where {@code gname} is a newly generated
* name. Automatically generated names are of the form
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
*
* @param target
* the object whose {@code run} method is invoked when this thread
* is started. If {@code null}, this classes {@code run} method does
* nothing.
*/
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
這個構造方法是傳入一個Runnable類型參數,然后在傳個init方法,init方法就是Thread類的初始化的地方,其中有一條語句很重要
this.target = target;
就是把傳入Runnable 類型參數賦給Thread成員變量target,疑問來了,就是在前面實例化了繼承Thread類的對象,它的構造方法就是一個無參的構造函數,在看源碼
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
在這個構造方法中給init方法傳入的第二個參數是null,也就是target=null,初始化了一個null的target,是不是很疑惑呀。好,在上源碼
/**
* 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)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
這是從寫了Runnable的run()方法,這里面就一個片段,意思是如果target不為null,也就是說我們傳入了一個Runnable對象,然后調用Runnable對象的run()方法,這樣是因為我們已在實現Runnable接口的類中以從寫了run()方法,如果target為null,意思就是通過new Thread而不傳入Runnable對象,然后就會調用繼承了Thread類的實現類中的run()方法。
在一個循環中創建線程多次運行可能會產生不同的結果,主要是線程調度機制是非確定性的。
[/size]
概念
線程,就是你寫的一個簡單的程序,運行的載體的就是線程,幫你完成一件事。我的簡單理解就是,線程就像洗衣機一樣,它的功能幫你完成洗衣服,猶豫你很懶堆了一個月的衣服,而此時要洗,肯定是一個洗衣機搞不定了哦,而你要趕時間要把這個一個衣服用最短的時間洗完,你就用多臺洗衣機來洗(純屬假設,如果你家是賣洗衣機的到可試試),剛才那情形就是傳說的多線程,就是多個任務同時運行,并起運行,就是并發了。
運行
你要用洗衣機洗衣服,前提是你已有臟衣服的存在,放在洗衣機里,然后插上電,打開洗衣開關,洗衣機就開始工作了哦。同樣在使用線程時,你要把你要打算做的事要事先編碼好,然后把它添加到線程的run()方法中。最后調用start()方法,ok! 跟洗衣機一樣工作了。隨著高科技和不斷創新的發展,洗衣機洗衣方式也是多種多樣,我見過兩種,一種是很古老的:就是洗衣和脫水時分開的那種,這種用起來很不方便,需要自己脫水(我很討厭這種洗發)。一種是很先進的:就是洗衣和脫水完美的融為一體,這樣很方便了,不要你親自來脫水(很喜歡這種洗發)。其實實現線程的方式也有兩種了。第一種就是很直接的使用Thread類搞定。繼承Thread 在run()方法中放入你要實現的功能。如下代碼
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("hello ! i'm from Thread");
}
}
在run方法中只是一句簡單的輸出語句。然后運行,代碼如下
public static void main(String [] args){
ThreadDemo demo=new ThreadDemo();
demo.start();
}
好了第一種線程運行方式搞定,接下來看第二種,就是實現Runnable接口了
public class ThreadDemo2 implements Runnable{
@Override
public void run() {
System.out.println("hello ! i'm from Runnable");
}
}
這種方式就是逼著你要實現run()方法了。其執行也略有不同了
public static void main(String [] args){
Thread demo=new Thread(new ThreadDemo2());
demo.start();
}
實現Runnable執行需要一個載體了,為什么了?看Runnable的源碼
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();
}
看起來是不是感覺so easy!它就一個run()方法,就是有裝任務的地方,就是沒發讓功能執行,所以需要求助外援了(純屬簡單理解,寫的不夠嚴謹)。但為什么要選Thread了,這要看源碼了
public class Thread implements Runnable {
............
}
看到沒,Thread 也實現了Runnable,看來他們有父子之情呀,從面向對象世界來說,子原則上都比父功能更強大豐富(現實中父子誰強誰弱這就無比復雜了,還是面向對象世界單純)。
所以子要繼承父的所有功能,看Thread如何實現,上源碼
/**
* Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
* {@code (null, target, gname)}, where {@code gname} is a newly generated
* name. Automatically generated names are of the form
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
*
* @param target
* the object whose {@code run} method is invoked when this thread
* is started. If {@code null}, this classes {@code run} method does
* nothing.
*/
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
這個構造方法是傳入一個Runnable類型參數,然后在傳個init方法,init方法就是Thread類的初始化的地方,其中有一條語句很重要
this.target = target;
就是把傳入Runnable 類型參數賦給Thread成員變量target,疑問來了,就是在前面實例化了繼承Thread類的對象,它的構造方法就是一個無參的構造函數,在看源碼
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
在這個構造方法中給init方法傳入的第二個參數是null,也就是target=null,初始化了一個null的target,是不是很疑惑呀。好,在上源碼
/**
* 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)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
這是從寫了Runnable的run()方法,這里面就一個片段,意思是如果target不為null,也就是說我們傳入了一個Runnable對象,然后調用Runnable對象的run()方法,這樣是因為我們已在實現Runnable接口的類中以從寫了run()方法,如果target為null,意思就是通過new Thread而不傳入Runnable對象,然后就會調用繼承了Thread類的實現類中的run()方法。
在一個循環中創建線程多次運行可能會產生不同的結果,主要是線程調度機制是非確定性的。
[/size]
總結
以上是生活随笔為你收集整理的重拾java之线程1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自助提取公积金(北京)
- 下一篇: linux flash擦除命令,Linu