溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java 多線程-改正不安全線程

發(fā)布時(shí)間:2020-08-05 19:21:42 來源:網(wǎng)絡(luò) 閱讀:230 作者:wx5d21d5e6e5ab1 欄目:編程語言

并發(fā):
同一個(gè)對象對多個(gè)線程同時(shí)操作
線程同步:一種等待機(jī)制,等待前面線程使用完再下一個(gè)線程使用
線程同步形成條件:形成隊(duì)列,加上鎖機(jī)制(synchronized)
同步塊:synchronized(具體對象){代碼};鎖定資源,一個(gè)線程一個(gè)線程的使用

搶票:

public class n {

public static void main(String[]args) throws InterruptedException
{
web wb=new web();
new Thread(wb,"a").start();
new Thread(wb,"b").start();
new Thread(wb,"c").start();
}
}

class web implements Runnable{
int num=10;
private boolean flag=true;
public void run()
{
while(flag)
{
    test();
}
}
public synchronized void test()//鎖的是對象的資源,即this成員,讓資源每次只被一個(gè)線程使用,然后下次靠cpu調(diào)度,而不讓
                            //一份資源同時(shí)被多個(gè)線程使用
{   if(num<0)
        {
    flag=false;
    return;
}
try {
    Thread.sleep(200);
}catch(InterruptedException e)
{
    e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->"+num--);
//線程不安全,可能都都是同一張票,可能票是負(fù)數(shù)
//負(fù)數(shù):當(dāng)還有1張票時(shí),三個(gè)線程同時(shí)進(jìn)入,都等待后,有兩個(gè)線程為負(fù)數(shù)
//相同票:線程有自己的工作臺,多個(gè)線程幾乎同時(shí)到達(dá),拷貝數(shù)據(jù)
}
}

取錢:

public class el {

public static void main(String[]args)
{
    account a=new account(100,"me");
    get g=new get(a,80,"she");
    get g2=new get(a,90,"he");
    g.start();
    g2.start();
}

}

//賬戶
class account {
int money;
String name;
public account(int money,String name)
{
    this.money=money;
    this.name=name;
}

}
//模擬取款

class get extends Thread
{
account a; //取錢的賬戶
int getmoney; //單個(gè)人取的錢數(shù)
int getall; //多個(gè)人取的總數(shù)

public get (account a,int getmoney,String name)
{
    super(name);//Thread可以設(shè)置name
    this.a=a;
    this.getmoney=getmoney;
}

public void run()
{
    test();
}
public /*synchronized */void test()//鎖定失敗,不應(yīng)該鎖this,而應(yīng)該鎖account
{//因?yàn)閍.money是account里的,總錢數(shù)應(yīng)該每次由一個(gè)線程調(diào)用,而不是多個(gè)線程
//使用同步塊鎖定
    if(a.money<=0)
    {
        return;
    }
synchronized(a){

    if(a.money-getmoney<0) //添加也沒用
    {
        return;
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    a.money-=getmoney;
    getall+=getmoney;
    System.out.println(this.getName()+"-->賬戶余額為:"+a.money);
    System.out.println(this.getName()+"-->取錢總數(shù)為:"+getall);
}
}
}

容器:

public class h {

public static void main(String[]args)
{
    List<String> list=new ArrayList<String>();

    for(int i=0;i<10000;i++)
    {
        new Thread(()->
        {synchronized(list) {
            list.add(Thread.currentThread().getName());}}
                ).start();
    }
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(list.size());

}
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI