package threadgroup;
汝城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,汝城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為汝城上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的汝城做網(wǎng)站的公司定做!
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();
}
}
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è)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é)果