java线程知识总结

线程的调度-合并

public class Test { 
        public static void main(String[] args) { 
                Thread t1 = new MyThread1(); 
                t1.start(); 

                for (int i = 0; i < 20; i++) { 
                        System.out.println("主线程第" + i + "次执行!"); 
                        if (i > 2) try { 
                                //t1线程合并到主线程中,主线程停止执行过程,转而执行t1线程,直到t1执行完毕后继续。 
                                t1.join(); 
                        } catch (InterruptedException e) { 
                                e.printStackTrace(); 
                        } 
                } 
        } 
} 

class MyThread1 extends Thread { 
        public void run() { 
                for (int i = 0; i < 10; i++) { 
                        System.out.println("线程1第" + i + "次执行!"); 
                } 
        } 
}
让步

public class Test { 
        public static void main(String[] args) { 
                Thread t1 = new MyThread1(); 
                Thread t2 = new Thread(new MyRunnable()); 

                t2.start(); 
                t1.start(); 
        } 
} 

class MyThread1 extends Thread { 
        public void run() { 
                for (int i = 0; i < 10; i++) { 
                        System.out.println("线程1第" + i + "次执行!"); 
                } 
        } 
} 

class MyRunnable implements Runnable { 
        public void run() { 
                for (int i = 0; i < 10; i++) { 
                        System.out.println("线程2第" + i + "次执行!"); 
                        Thread.yield(); 
                } 
        } 
}

交互

public class ThreadA { 
    public static void main(String[] args) { 
        ThreadB b = new ThreadB(); 
        //启动计算线程 
        b.start(); 
        //线程A拥有b对象上的锁。线程为了调用wait()或notify()方法,该线程必须是那个对象锁的拥有者 
        synchronized (b) { 
            try { 
                System.out.println("等待对象b完成计算。。。"); 
                //当前线程A等待 
                b.wait(); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("b对象计算的总和是:" + b.total); 
        } 
    } 
}

public class ThreadB extends Thread { 
    int total; 

    public void run() { 
        synchronized (this) { 
            for (int i = 0; i < 101; i++) { 
                total += i; 
            } 
            //(完成计算了)唤醒在此对象监视器上等待的单个线程,在本例中线程A被唤醒 
            notify(); 
        } 
    } 
}

lock

import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 

/** 
* Java线程:锁 
* 
* @author leizhimin 2009-11-5 10:57:29 
*/ 
public class Test { 
        public static void main(String[] args) { 
                //创建并发访问的账户 
                MyCount myCount = new MyCount("95599200901215522", 10000); 
                //创建一个锁对象 
                Lock lock = new ReentrantLock(); 
                //创建一个线程池 
                ExecutorService pool = Executors.newCachedThreadPool(); 
                //创建一些并发访问用户,一个信用卡,存的存,取的取,好热闹啊 
                User u1 = new User("张三", myCount, -4000, lock); 
                User u2 = new User("张三他爹", myCount, 6000, lock); 
                User u3 = new User("张三他弟", myCount, -8000, lock); 
                User u4 = new User("张三", myCount, 800, lock); 
                //在线程池中执行各个用户的操作 
                pool.execute(u1); 
                pool.execute(u2); 
                pool.execute(u3); 
                pool.execute(u4); 
                //关闭线程池 
                pool.shutdown(); 
        } 
} 

/** 
* 信用卡的用户 
*/ 
class User implements Runnable { 
        private String name;                //用户名 
        private MyCount myCount;        //所要操作的账户 
        private int iocash;                 //操作的金额,当然有正负之分了 
        private Lock myLock;                //执行操作所需的锁对象 

        User(String name, MyCount myCount, int iocash, Lock myLock) { 
                this.name = name; 
                this.myCount = myCount; 
                this.iocash = iocash; 
                this.myLock = myLock; 
        } 

        public void run() { 
                //获取锁 
                myLock.lock(); 
                //执行现金业务 
                System.out.println(name + "正在操作" + myCount + "账户,金额为" + iocash + ",当前金额为" + myCount.getCash()); 
                myCount.setCash(myCount.getCash() + iocash); 
                System.out.println(name + "操作" + myCount + "账户成功,金额为" + iocash + ",当前金额为" + myCount.getCash()); 
                //释放锁,否则别的线程没有机会执行了 
                myLock.unlock(); 
        } 
} 

/** 
* 信用卡账户,可随意透支 
*/ 
class MyCount { 
        private String oid;         //账号 
        private int cash;             //账户余额 

        MyCount(String oid, int cash) { 
                this.oid = oid; 
                this.cash = cash; 
        } 

        public String getOid() { 
                return oid; 
        } 

        public void setOid(String oid) { 
                this.oid = oid; 
        } 

        public int getCash() { 
                return cash; 
        } 

        public void setCash(int cash) { 
                this.cash = cash; 
        } 

        @Override 
        public String toString() { 
                return "MyCount{" + 
                                "oid='" + oid + '\'' + 
                                ", cash=" + cash + 
                                '}'; 
        } 
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值