首先,你同步的是具體的某個Test實例, 對于那個實例來說,實際上只有一個線程訪問了那個代碼塊,但是sum和other卻是多個線程同時去進行訪問,實際上這是不安全的,如果你想實現(xiàn)每次都輸出10000的效果,那么正確的應(yīng)該是在Test.class上加鎖,而不是獲取Test實例的鎖,修改后的代碼如下:
網(wǎng)站設(shè)計制作過程拒絕使用模板建站;使用PHP+MYSQL原生開發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺管理系統(tǒng);成都網(wǎng)站制作、成都做網(wǎng)站收費合理;免費進行網(wǎng)站備案等企業(yè)網(wǎng)站建設(shè)一條龍服務(wù).我們是一家持續(xù)穩(wěn)定運營了十余年的成都創(chuàng)新互聯(lián)網(wǎng)站建設(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é)果
照著下面的例子改一下:
/**
* 實現(xiàn)Runnable接口的類
*
* @author leizhimin 2008-9-13 18:12:10
*/
publicclass DoSomethingimplements Runnable {
private String name;
public DoSomething(String name) {
this.name = name;
}
publicvoid run() {
for (int i = 0; i 5; i++) {
for (long k = 0; k 100000000; k++) ;
System.out.println(name + ": " + i);
}
}
}
/**
* 測試Runnable類實現(xiàn)的多線程程序
*
* @author leizhimin 2008-9-13 18:15:02
*/
publicclass TestRunnable {
publicstaticvoid main(String[] args) {
DoSomething ds1 = new DoSomething("阿三");
DoSomething ds2 = new DoSomething("李四");
Thread t1 = new Thread(ds1);
Thread t2 = new Thread(ds2);
t1.start();
t2.start();
}
}
執(zhí)行結(jié)果:
李四: 0
阿三: 0
李四: 1
阿三: 1
李四: 2
李四: 3
阿三: 2
李四: 4
阿三: 3
阿三: 4
Process finished with exit code 0
2、擴展Thread類實現(xiàn)的多線程例子
/**
* 測試擴展Thread類實現(xiàn)的多線程程序
*
* @author leizhimin 2008-9-13 18:22:13
*/
publicclass TestThreadextends Thread{
public TestThread(String name) {
super(name);
}
publicvoid run() {
for(int i = 0;i5;i++){
for(long k= 0; k 100000000;k++);
System.out.println(this.getName()+" :"+i);
}
}
publicstaticvoid main(String[] args) {
Thread t1 = new TestThread("阿三");
Thread t2 = new TestThread("李四");
t1.start();
t2.start();
}
}
執(zhí)行結(jié)果:
阿三 :0
李四 :0
阿三 :1
李四 :1
阿三 :2
李四 :2
阿三 :3
阿三 :4
李四 :3
李四 :4
Process finished with exit code 0
package?com.zhidao20161220;
public?class?Main?{
private?static?int?count=0;
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
for(int?i=0;i10;i++)
{
String?name?=?"人員"+(i+1);
ManThread?manThread?=?new?ManThread(name);
manThread.start();
}
}
public?static?synchronized?void?go(String?name)
{
count++;
System.out.println("線程"+Thread.currentThread().getName()+"執(zhí)行,"+name+"正在通過山洞");
try?{
if(count=10)
{
System.out.println("全員通過程序結(jié)束");
}
Thread.sleep(5000);
}?catch?(InterruptedException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
package?com.zhidao20161220;
public?class?ManThread?extends?Thread?{
private?String?name;
public?ManThread(String?name)
{
this.name?=?name;
}
public?void?run()?{
Main.go(name);
}
}
/*
class A extends Thread
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}
class B extends Thread
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}
class C extends Thread
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}
public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}*/
public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
Thread ta = new Thread(a);
Thread tb = new Thread(b);
Thread tc = new Thread(c);
ta.start();
tb.start();
tc.start();
}
}
class A implements Runnable
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}
class B implements Runnable
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}
class C implements Runnable
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}
案例一的兩種方法已經(jīng)寫好;現(xiàn)在有事,晚上再把案例二代碼寫一下,應(yīng)該沒關(guān)系吧!
抱歉,是一個線程類,我看錯了,晚上重發(fā)一下代碼!
package?know;
import?java.io.BufferedReader;
import?java.io.FileInputStream;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.util.HashMap;
import?java.util.Map;
import?java.util.Scanner;
public?class?T20?{
public?static?void?main(String[]?args)?throws?IOException?{
Scanner?s=new?Scanner(System.in);
String?username=null;
String?password=null;
System.out.println("輸入用戶名:");
while(true){
if(username==null){
username=s.next();
System.out.println("輸入密碼:");
}else{
password=s.next();
}
if(password!=null){
s.close();
break;
}
}
BufferedReader?br=null;
MapString,?String?map=new?HashMapString,?String();
try{
br=new?BufferedReader(new?InputStreamReader(new?FileInputStream("d:/test.txt")));
String?temp=null;???
while((temp=br.readLine())!=null){
String[]?ss=temp.split("=");
map.put(ss[0],?ss[1]);
}
}catch(IOException?e){
throw?e;
}finally{
if(br!=null)
br.close();
}
String?u=map.get("userName");
String?p=map.get("password");
if(u.equals(username)p.equals(password)){
System.out.println("登錄成功");
}else{
System.out.println("用戶名或密碼錯誤");
}
}
}
package?know;
public?class?T21?{
public?static?void?main(String[]?args)?throws?InterruptedException?{
String[]?persons=new?String[]{"甲","乙","丙","丁","午","己","庚","辛","壬","癸"};
for(int?i=0;ipersons.length;i++){
System.out.println(persons[i]+"正在過山洞");
Thread.sleep(5000);
}
}
}
最后一個百度搜一個就行了,肯定比我畫的好
網(wǎng)頁名稱:java多線程編程題代碼 java多線程編程題代碼怎么寫
鏈接分享:http://m.kartarina.com/article14/dodeode.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作、網(wǎng)頁設(shè)計公司、ChatGPT、標(biāo)簽優(yōu)化、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)