package threadgroup;
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)樊城,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多線程測試!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("線程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("線程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("線程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好嗎?");
System.out.println("正在進(jìn)行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆繼超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一個主進(jìn)程");
System.out.println("正在運(yùn)行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在創(chuàng)建一個進(jìn)程");
t2.start();
try {
System.out.println("使他進(jìn)入第一個睡眠狀態(tài)");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一個進(jìn)程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("進(jìn)程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二個進(jìn)程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
線程用到Thread或者Runnable接口(Thread也操作了Runnable接口)
繼承了Thread類后需要重載其run方法,在方法里寫你需要完成的事情,開始線程是調(diào)用其start方法。
操作Runnable接口必須實(shí)現(xiàn)其run方法,在方法里寫你需要完成的事情,Runnable接口沒有start方法,所以啟動線程還是需要依靠Thread類 new Thread(Runnable runnable).start();
一般項(xiàng)目中多是操作接口,因?yàn)轭愔荒軉卫^承,接口可以操作多個。
首先,你同步的是具體的某個Test實(shí)例, 對于那個實(shí)例來說,實(shí)際上只有一個線程訪問了那個代碼塊,但是sum和other卻是多個線程同時去進(jìn)行訪問,實(shí)際上這是不安全的,如果你想實(shí)現(xiàn)每次都輸出10000的效果,那么正確的應(yīng)該是在Test.class上加鎖,而不是獲取Test實(shí)例的鎖,修改后的代碼如下:
public?class?Test?extends?Thread?{
public?static?int?sum?=?10000;
public?static?int?other?=?0;
public?void?getMoney()?{
synchronized?(Test.class)?{
System.out.println(Thread.currentThread().getName()?+?"?開始執(zhí)行");
sum?=?sum?-?100;
System.out.println("sum-100");
other?=?other?+?100;
System.out.println("other+100");
System.out.println(sum?+?other);
System.out.println(Thread.currentThread().getName()?+?"?執(zhí)行完成");
}
}
public?void?run()?{
getMoney();
}
public?static?void?main(String[]?agrs)?{
Thread?t[]?=?new?Thread[10];
for?(int?i?=?0;?i?=?9;?i++)?{
t[i]?=?new?Test();
t[i].start();
}
}
}
// 上面代碼能得到你的結(jié)果
這段代碼的功能是顯示各個時區(qū)當(dāng)前時鐘。
TimerListener是一個接口,有一個timeElapsed方法,目的是根據(jù)當(dāng)前的時間繪制時鐘,并刷新顯示。
Timer繼承Thread類,實(shí)現(xiàn)了run方法。run方法中,休眠指定的時間,并調(diào)用TimerListener的timeElapsed方法。如上例就是每休眠1S調(diào)用一次,所以看到的結(jié)果就是每1S繪制的時鐘會更新一次。
ClockCanvas繼承JPanel并實(shí)現(xiàn)了TimerListener接口,在構(gòu)造方法中,根據(jù)指定的時區(qū)得到calendar實(shí)例。并開啟線程Timer。
重寫了paintComponent方法,在該方法中,首先繪制了一個圓,然后分別繪制時針、分針和秒針。
時針顏色為紅色,分針為黃色,秒針為藍(lán)色。在時鐘下面繪制了城市,顏色為黑色。
是特殊的線程,一般用于在后臺為其他線程提供服務(wù).
isDaemon():判斷一個線程是否為守護(hù)線程.
set Daemon():設(shè)置一個線程為守護(hù)線程.
Thread類和Runnable接口
Thread類
類Thread在包java.lang中定義,它的構(gòu)造方法如下:
public Thread();
public Thread(Runnable target);
public Thread(Runnable target,String name);
public Thread(String name);
public Thread(ThreadGroup group,Runnable target);
public Thread(ThreadGroup group, String name);
主要方法
isActive() 判斷是否處于執(zhí)行狀態(tài)
Suspend() 暫停執(zhí)行
reSume 恢復(fù)執(zhí)行
start() 開始執(zhí)行
Stop() 停止執(zhí)行
sleep() 睡眠
run() 程序體
yield() 向其他線程退讓運(yùn)行權(quán)
線程優(yōu)先級
Public statuc final int MAX_PRIORITY最高優(yōu)先級,10
Public statuc final int MIN_PRIORITY最低優(yōu)先級,1
Public statuc final int NORM_PRIORITY普通優(yōu)先級,5
Runnable接口
Runnable接口中只定義了一個方法run()作為線程體,
void run()
Java的線程是通過java.lang.Thread類來實(shí)現(xiàn)的。
VM啟動時會有一個由主方法(public static void main(){})所定義的線程。
可以通過創(chuàng)建Thread的實(shí)例來創(chuàng)建新的線程。
每個線程都是通過某個特定的Thread對象所對應(yīng)的方法run()來完成其操作的,方法run()稱為線程體。
通過調(diào)用Thread類的start()方法來啟動一個線程
Java里面實(shí)現(xiàn)多線程,有2個方法
1 繼承 Thread類,比如
class MyThread extends Thread {
public void run() {
// 這里寫上線程的內(nèi)容
}
public static void main(String[] args) {
// 使用這個方法啟動一個線程
new MyThread().start();
}
}
2 實(shí)現(xiàn) Runnable接口
class MyThread implements Runnable{
public void run() {
// 這里寫上線程的內(nèi)容
}
public static void main(String[] args) {
// 使用這個方法啟動一個線程
new Thread(new MyThread()).start();
}
}
一般鼓勵使用第二種方法,應(yīng)為Java里面只允許單一繼承,但允許實(shí)現(xiàn)多個接口。第二個方法更加靈活。
當(dāng)前文章:JAVA線程內(nèi)代碼,線程的代碼
文章起源:http://chinadenli.net/article30/dsshgso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、移動網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、虛擬主機(jī)、Google、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)