多线程详解

2021-03-03 19:29

阅读:307

标签:sum   消费   的区别   i++   system   dea   boolean   raw   catch   

1.三个多线程可能引起的问题

package other;

public class UnSafeTicker implements Runnable {
    private int ticket=10;
    boolean flag=true;
    @Override
    public void run() {
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized  void buy() throws InterruptedException {
        if(ticket){
            flag=false;
            return;
        }
        System.out.println(Thread.currentThread().getName()+"买了"+ticket+"张票");
        ticket--;
        Thread.sleep(1000);
    }

    public static void main(String[] args) {
        UnSafeTicker unSafeTicker=new UnSafeTicker();
        Thread thread=new Thread(unSafeTicker,"a");
        Thread thread1=new Thread(unSafeTicker,"b");
        Thread thread2=new Thread(unSafeTicker,"c");
        thread.start();
        thread1.start();
        thread2.start();

    }
}
package other;

import javax.naming.Name;
import javax.sound.midi.Soundbank;

public class UnsafeBank {

    public static void main(String[] args) {
        Acount acount=new Acount(150,"结婚基金");
        Drawer drawer=new Drawer(acount,50,"zs");
        Drawer drawer1=new Drawer(acount,100,"ls");
        Thread thread=new Thread(drawer);
        Thread thread1=new Thread(drawer1);
        thread1.start();
        thread.start();
    }
}
class Acount{
    int count ;
    String name;
    public Acount(int count,String name){
        this.name=name;
        this.count=count;
    }
}
class Drawer extends Thread{
    Acount acount;
    int needMoney;
    int nowMoney;

    public Drawer(Acount acount,int needMoney,String name){
        super(name);
        this.acount=acount;
        this.needMoney=needMoney;
    }

    @Override
    public void run() {
        synchronized (acount){
            if(acount.countneedMoney){
                System.out.println("钱不够,无法取");

                return;
            }else{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                acount.count-=needMoney;
                System.out.println(Thread.currentThread().getName()+"取了"+needMoney);
                System.out.println("现在有"+acount.count);
            }
        }

    }
}
package other;

import org.springframework.aop.ThrowsAdvice;

import java.util.ArrayList;
import java.util.List;

public class UnsafeList {
    public static void main(String[] args) throws InterruptedException {
        List list=new ArrayList();
        for (int i = 0; i ) {
            new Thread(()->{
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(1000);
        System.out.println(list.size());
    }
}

2.死锁问题

package other;



import org.apache.ibatis.annotations.MapKey;

import java.awt.*;

public class SiSuo extends Thread  {
    public static void main(String[] args) {
        MakeUp makeUp=new MakeUp(0,"zs");
        MakeUp makeUp1=new MakeUp(1,"LS");
        makeUp.start();
        makeUp1.start();
    }
}
class Mirror{

}
class Pen{

}
class MakeUp extends Thread{
    int choice;
    static Mirror mirror=new Mirror();
    static Pen pen=new Pen();
    private String girlname;
    MakeUp(int choice,String girlname){
        this.choice=choice;
        this.girlname=girlname;
    }
    @Override
    public void run() {
        makeup();
    }
    public void makeup(){
        if(choice==0){
            synchronized (mirror){
                System.out.println(Thread.currentThread().getName()+"拥有mirror");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            synchronized (pen){
                System.out.println(Thread.currentThread().getName()+"拥有pen");
            }
        }else{
            synchronized (pen){
                System.out.println(Thread.currentThread().getName()+"拥有pen");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            synchronized (mirror){
                System.out.println(Thread.currentThread().getName()+"拥有mirror");
            }
        }
    }
}

3.copyonwritearraylist与arraylist的区别

package other;

import java.util.concurrent.CopyOnWriteArrayList;

public class DeadLock {
    public static void main(String[] args) throws InterruptedException {
        CopyOnWriteArrayList copyOnWriteArrayList=new CopyOnWriteArrayList();
        for (int i = 0; i ) {
            new Thread(()->{
                copyOnWriteArrayList.add(Thread.currentThread().getName());
            }).start();
        }
        Thread.sleep(1000);
        System.out.println(copyOnWriteArrayList.size());
    }
}

4.lock的问题,与synchronized的区别,主动地开始锁

package other;

import org.eclipse.sisu.space.LoadedClass;

import java.util.concurrent.locks.ReentrantLock;

public class LockTest{



    public static void main(String[] args) {
        LockT lockT=new LockT();
        Thread thread=new Thread(lockT);
        Thread thread1=new Thread(lockT);
        Thread thread2=new Thread(lockT);
        thread.start();
        thread1.start();
        thread2.start();
    }
}
class LockT implements Runnable{
    private static ReentrantLock lock=new ReentrantLock();
    private static int tickecnum=10;
    @Override
    public void  run() {
        buy();
    }
    public void buy(){

        while(true){
           try {
               lock.lock();
               if(tickecnum>0){
                   System.out.println(Thread.currentThread().getName()+"=="+tickecnum);
                   tickecnum--;
               }
               Thread.sleep(1000);
           }catch (Exception e){
               e.printStackTrace();
           }finally {
               lock.unlock();

           }
        }

    }

}

5.生产者消费者的问题

package other;

public class ConsumerAndProductorTest {
    public static void main(String[] args) {
        Cache cache=new Cache();
        Consumer consumer=new Consumer(cache);
        Productor productor=new Productor(cache);
        consumer.start();
        productor.start();
    }
}
class Consumer extends Thread{
    private Cache cache;

    public Consumer(Cache cache) {
        this.cache = cache;
    }
    @Override
    public void run() {
        for (int i = 0; i ) {
            Chicken chicken=cache.pop();
            System.out.println("消费"+chicken.getId()+"只鸡");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class  Productor extends Thread{
    private Cache cache;

    public Productor(Cache cache) {
        this.cache = cache;
    }

    @Override
    public void run() {
        for (int i = 0; i ) {
           Chicken chicken=new Chicken(i);
           cache.push(chicken);
            System.out.println("生产"+chicken.getId()+"只鸡");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Cache{
    int count;
    Chicken[] chickens=new Chicken[10];
    public synchronized void push(Chicken chicken){
        if(count==10){

                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }else {
                    chickens[count]=chicken;
                    count++;
                    this.notifyAll();
        }
    }
    public synchronized Chicken pop(){
        if(count==0){

                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        count--;
        Chicken chicken=chickens[count];
        this.notifyAll();
        return chicken;

    }
}
class Chicken{
    private int id;

    public int getId() {
        return id;
    }

    public Chicken(int id) {
        this.id = id;
    }
}

 

多线程详解

标签:sum   消费   的区别   i++   system   dea   boolean   raw   catch   

原文地址:https://www.cnblogs.com/afeime/p/14383584.html


评论


亲,登录后才可以留言!