java线程高级(代码)


package com.dasenlin.gaoji;

public class waitDemo {
    public static void main(String[] args) {
        ThreadB b = new ThreadB();
        b.start();
        synchronized(b){//需要b这个对象的这把锁(目前是主线程等待b执行完成)
            try {
                System.out.println("等待对象完成执行");
                b.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("b对象计算的总和是:"+b.total);
        }
    }
}
class ThreadB extends Thread{
    int total;

    @Override
    public void run() {
        synchronized(this){
            for(int i=0;i<101;i++){
                total+=i;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //计算完成后通知等待的线程,可以取锁
            notify();
        }
    }
    
}


 

package com.dasenlin.gaoji;

public class WaitDemo2 {
    
    public static void main(String[] args) {
        final String flag = "go beijing";
        Thread driver = new Thread(){

            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    System.out.println("[司机]正在开车去目的地");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("[司机]抵达目的地了");
                synchronized(flag){
                    System.out.println("[司机]叫醒乘客");
                    flag.notify();
                }
            }
            
        };
        Thread passenger = new Thread(){
            @Override
            public void run(){
                synchronized(flag){
                    System.out.println("[乘客]告诉司机开始出发了");
                    driver.start();//司机启动
                    try {
                        System.out.println("[乘客]准备开始睡觉额");
                        flag.wait();
                        System.out.println("[乘客]抵达");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
            }
        };
        passenger.start();//乘客启动;
    }
}

 


package com.dasenlin.gaoji;

public class WaitDemo3 {
    public static void main(String[] args) {
        Godown godown = new Godown(30);
        Consumer c1 = new Consumer(50,godown);
        Consumer c2 = new Consumer(20,godown);
        Consumer c3 = new Consumer(30,godown);
        Producer p1 = new Producer(10, godown);
        Producer p2 = new Producer(10, godown);
        Producer p3 = new Producer(10, godown);
        Producer p4 = new Producer(10, godown);
        Producer p5 = new Producer(10, godown);
        Producer p6 = new Producer(10, godown);
        Producer p7 = new Producer(80, godown);
        c1.start();c2.start();c3.start();
        p1.start();p2.start();p3.start();p4.start();p5.start();p6.start();p7.start();
    }
}
class Godown{
    public static final int max_size=100;
    public int curnum;
    
    public Godown() {

    }
    public Godown(int curnum) {
        this.curnum = curnum;
    }
    public synchronized void produce(int neednum){
        while((neednum + curnum)>max_size){
            System.out.println("要生产的产品数量"+neednum+"超过剩余库存量"+(max_size-curnum)+",暂时不能执行生产任务!");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        curnum +=neednum;
        System.out.println("已经生产了"+neednum+"个产品,现在仓储量为"+curnum);
        notifyAll();
    }
    public synchronized void consume(int neednum){
        while(curnum<neednum){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        curnum-=neednum;
        System.out.println("消费了"+neednum+"个产品,现在仓储量为"+curnum);
        notifyAll();
    }
}

class Producer extends Thread{
    private int neednum;
    private Godown godown;
    
    public Producer(int neednum,Godown godown){
        this.neednum = neednum;
        this.godown = godown;
    }
    @Override
    public void run(){
        godown.produce(neednum);
    }
}
class Consumer extends Thread{
    private int neednum;
    private Godown godown;
    
    public Consumer(int neednum,Godown godown){
        this.neednum = neednum;
        this.godown = godown;
    }
    @Override
    public void run(){
        godown.consume(neednum);
    }
}


package com.dasenlin.gaoji;

public class ThreadDemo {
    public static void main(String[] args) {
        Thread t1 = new MyCommon();
        Thread t2 = new Thread(new MyDaemon());
        t2.setDaemon(true);
        t2.start();
        t1.start();
    }
}
class MyCommon extends Thread{

    @Override
    public void run() {
        for(int i=0;i<5;i++){
            System.out.println("线程1第"+i+"次执行!");
            try {
                Thread.sleep(7);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
}
class MyDaemon implements Runnable{

    @Override
    public void run() {
        for(long i=0;i<9999999L;i++){
            System.out.println("后台线程第"+i+"次执行!");
            try {
                Thread.sleep(7);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


package com.dasenlin.gaoji;
/**
 * 
 */
public class ErrorDemo {
    
    static class SellTicket implements Runnable{
        private int ticket=10;
        private String lock="";  //标记锁
        @Override
        public void run() {
            while(true){
                /*synchronized(this){ //对象锁 SellTicket.class
                    if(this.ticket>0){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"剩余:" + --this.ticket);
                    }else{
                        break;
                    }
                }//对象锁*/                
                synchronized(lock){ //标记锁
                    if(this.ticket>0){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"剩余:" + --this.ticket);
                    }else{
                        break;
                    }
                }// 标记锁
            }
        }
        
    } 
    
    
    public static void main(String[] args) {
        SellTicket sk = new SellTicket();
        Thread t1 = new Thread(sk);
        Thread t2 = new Thread(sk);
        Thread t3 = new Thread(sk);
               t1.start();
               t2.start();
               t3.start();
    }
}


package com.dasenlin.gaoji;

public class DeadLockDemo {
    
    public static void main(String[] args) {
        System.out.println("主线程开始");
        final StringBuffer buffer = new StringBuffer("ABCD");
        Thread t = new Thread(){
            @Override
            public void run() {
                System.out.println("子线程开始");
                synchronized(buffer){
                    buffer.reverse();
                    System.out.println("子:"+buffer);
                }
                System.out.println("子线程结束");
            }
            
        };
        t.start();//启动了
        synchronized(buffer){
            System.out.println("进入同步锁");
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(buffer);
        }
        
        System.out.println("主线程结束");
    }
}

 


package com.dasenlin.gaoji;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDemo {
    
    public static void main(String[] args) {
        try {
            ExecutorService pool = Executors.newFixedThreadPool(2);
            Callable c1 = new MyCallable("A");
            Callable c2 = new MyCallable("B");
            Future f1 = pool.submit(c1);
            Future f2 = pool.submit(c2);
            System.out.println(">>>"+f1.get().toString());
            System.out.println(">>>"+f2.get().toString());
            pool.shutdown();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class MyCallable implements Callable{
    private String oid;
    private int total;
    
    MyCallable(String oid){
        this.oid = oid;
    }
    @Override
    public Object call() throws Exception {
        for(int i=0;i<101;i++){
            total+=i;
            Thread.sleep(10);
        }
        return oid+":"+this.total;
    }
    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值