package threadgroup;
成都創(chuàng)新互聯(lián)主營(yíng)鄢陵網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),鄢陵h5重慶小程序開發(fā)公司搭建,鄢陵網(wǎng)站營(yíng)銷推廣歡迎鄢陵等地區(qū)企業(yè)咨詢
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();
}
}
因?yàn)槟鉵ew了兩次
試著在Task類內(nèi)創(chuàng)建一個(gè)對(duì)象 然后鎖住這個(gè)對(duì)象
package a.b.test;
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)采納!
給你一個(gè)經(jīng)典的例子。run里面放空循環(huán)來觀察多線程是不合理的,空循環(huán)消耗時(shí)序極小,用sleep來間隔時(shí)間才是合理的。
class?RunnableDemo?implements?Runnable?{
private?Thread?t;
private?String?threadName;
RunnableDemo(?String?name)?{
threadName?=?name;
System.out.println("Creating?"?+??threadName?);
}
public?void?run()?{
System.out.println("Running?"?+??threadName?);
try?{
for(int?i?=?4;?i??0;?i--)?{
System.out.println("Thread:?"?+?threadName?+?",?"?+?i);
//?Let?the?thread?sleep?for?a?while.
Thread.sleep(50);
}
}catch?(InterruptedException?e)?{
System.out.println("Thread?"?+??threadName?+?"?interrupted.");
}
System.out.println("Thread?"?+??threadName?+?"?exiting.");
}
public?void?start?()?{
System.out.println("Starting?"?+??threadName?);
if?(t?==?null)?{
t?=?new?Thread?(this,?threadName);
t.start?();
}
}
}
public?class?TestThread?{
public?static?void?main(String?args[])?{
RunnableDemo?R1?=?new?RunnableDemo(?"Thread-1");
R1.start();
RunnableDemo?R2?=?new?RunnableDemo(?"Thread-2");
R2.start();
}???
}
首先,你同步的是具體的某個(gè)Test實(shí)例, 對(duì)于那個(gè)實(shí)例來說,實(shí)際上只有一個(gè)線程訪問了那個(gè)代碼塊,但是sum和other卻是多個(gè)線程同時(shí)去進(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é)果
Java 給多線程編程提供了內(nèi)置的支持。 一條線程指的是進(jìn)程中一個(gè)單一順序的控制流,一個(gè)進(jìn)程中可以并發(fā)多個(gè)線程,每條線程并行執(zhí)行不同的任務(wù)。
新建狀態(tài):
使用 new 關(guān)鍵字和 Thread 類或其子類建立一個(gè)線程對(duì)象后,該線程對(duì)象就處于新建狀態(tài)。它保持這個(gè)狀態(tài)直到程序 start() 這個(gè)線程。
就緒狀態(tài):
當(dāng)線程對(duì)象調(diào)用了start()方法之后,該線程就進(jìn)入就緒狀態(tài)。就緒狀態(tài)的線程處于就緒隊(duì)列中,要等待JVM里線程調(diào)度器的調(diào)度。
運(yùn)行狀態(tài):
如果就緒狀態(tài)的線程獲取 CPU 資源,就可以執(zhí)行 run(),此時(shí)線程便處于運(yùn)行狀態(tài)。處于運(yùn)行狀態(tài)的線程最為復(fù)雜,它可以變?yōu)樽枞麪顟B(tài)、就緒狀態(tài)和死亡狀態(tài)。
阻塞狀態(tài):
如果一個(gè)線程執(zhí)行了sleep(睡眠)、suspend(掛起)等方法,失去所占用資源之后,該線程就從運(yùn)行狀態(tài)進(jìn)入阻塞狀態(tài)。在睡眠時(shí)間已到或獲得設(shè)備資源后可以重新進(jìn)入就緒狀態(tài)。可以分為三種:
等待阻塞:運(yùn)行狀態(tài)中的線程執(zhí)行 wait() 方法,使線程進(jìn)入到等待阻塞狀態(tài)。
同步阻塞:線程在獲取 synchronized 同步鎖失敗(因?yàn)橥芥i被其他線程占用)。
其他阻塞:通過調(diào)用線程的 sleep() 或 join() 發(fā)出了 I/O 請(qǐng)求時(shí),線程就會(huì)進(jìn)入到阻塞狀態(tài)。當(dāng)sleep() 狀態(tài)超時(shí),join() 等待線程終止或超時(shí),或者 I/O 處理完畢,線程重新轉(zhuǎn)入就緒狀態(tài)。
死亡狀態(tài):
一個(gè)運(yùn)行狀態(tài)的線程完成任務(wù)或者其他終止條件發(fā)生時(shí),該線程就切換到終止?fàn)顟B(tài)。
本文題目:java多線程的項(xiàng)目代碼 java多線程實(shí)現(xiàn)的代碼
URL地址:http://www.chinadenli.net/article2/doogsic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、營(yíng)銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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í)需注明來源: 創(chuàng)新互聯(lián)