package a.b.test;

做網(wǎng)站、成都網(wǎng)站制作介紹好的網(wǎng)站是理念、設(shè)計(jì)和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計(jì)理念、多方位的設(shè)計(jì)風(fēng)格、經(jīng)驗(yàn)豐富的設(shè)計(jì)團(tuán)隊(duì)。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營(yíng)銷(xiāo)思維進(jìn)行網(wǎng)站設(shè)計(jì)、采用先進(jìn)技術(shù)開(kāi)源代碼、注重用戶(hù)體驗(yàn)與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶(hù)的方式做到創(chuàng)意性的視覺(jué)化效果。
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Calculate1000 implements CallableInteger{
public Calculate1000(){}
public Calculate1000(int a, int b){
this.a = a;
this.b = b;
}
int a;
int b;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//同步
Calculate1000 ca1 = new Calculate1000();
Date ds1 = new Date();
int result = 0;
for(int i = 1 ; i = 1000 ; i++){
result = ca1.add(i, result);
}
System.out.println(result);
System.out.println("同步用時(shí)" + (new Date().getTime() - ds1.getTime()) + "MS");
//異步
Date ds2 = new Date();
result = 0;
ExecutorService es = Executors.newFixedThreadPool(2);
FutureInteger future1 = es.submit(new Calculate1000(1,500));
FutureInteger future2 = es.submit(new Calculate1000(501,1000));
result = future1.get() + future2.get();
System.out.println(result);
System.out.println("異步用時(shí)" + (new Date().getTime() - ds2.getTime()) + "MS");
es.shutdown();
}
private int add(int a, int b) throws Exception{
Thread.sleep(10);
return a + b;
}
@Override
public Integer call() throws Exception {
int res = 0;
for(int i = a ; i = b ; i++){
res = this.add(res, i);
}
return res;
}
}
樓主你試一下這段代碼行不行,行的話請(qǐng)采納!
線程用到Thread或者Runnable接口(Thread也操作了Runnable接口)
繼承了Thread類(lèi)后需要重載其run方法,在方法里寫(xiě)你需要完成的事情,開(kāi)始線程是調(diào)用其start方法。
操作Runnable接口必須實(shí)現(xiàn)其run方法,在方法里寫(xiě)你需要完成的事情,Runnable接口沒(méi)有start方法,所以啟動(dòng)線程還是需要依靠Thread類(lèi) new Thread(Runnable runnable).start();
一般項(xiàng)目中多是操作接口,因?yàn)轭?lèi)只能單繼承,接口可以操作多個(gè)。
package threadgroup;
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("多線程測(cè)試!\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("第一個(gè)主進(jìn)程");
System.out.println("正在運(yùn)行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在創(chuàng)建一個(gè)進(jìn)程");
t2.start();
try {
System.out.println("使他進(jìn)入第一個(gè)睡眠狀態(tài)");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一個(gè)進(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("退出第二個(gè)進(jìn)程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
這段代碼的功能是顯示各個(gè)時(shí)區(qū)當(dāng)前時(shí)鐘。
TimerListener是一個(gè)接口,有一個(gè)timeElapsed方法,目的是根據(jù)當(dāng)前的時(shí)間繪制時(shí)鐘,并刷新顯示。
Timer繼承Thread類(lèi),實(shí)現(xiàn)了run方法。run方法中,休眠指定的時(shí)間,并調(diào)用TimerListener的timeElapsed方法。如上例就是每休眠1S調(diào)用一次,所以看到的結(jié)果就是每1S繪制的時(shí)鐘會(huì)更新一次。
ClockCanvas繼承JPanel并實(shí)現(xiàn)了TimerListener接口,在構(gòu)造方法中,根據(jù)指定的時(shí)區(qū)得到calendar實(shí)例。并開(kāi)啟線程Timer。
重寫(xiě)了paintComponent方法,在該方法中,首先繪制了一個(gè)圓,然后分別繪制時(shí)針、分針和秒針。
時(shí)針顏色為紅色,分針為黃色,秒針為藍(lán)色。在時(shí)鐘下面繪制了城市,顏色為黑色。
新聞標(biāo)題:漂亮的線程代碼java,編譯線程數(shù)
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article10/dsgeogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、自適應(yīng)網(wǎng)站、Google、用戶(hù)體驗(yàn)、響應(yīng)式網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)