java线程基础

来源:互联网 发布:mac 怎么启动jenkins 编辑:程序博客网 时间:2024/06/07 07:55
1.线程:程序中的不同执行路径
    进程:静态概念,机器上的一个class文件或者一个exe文件都可以被称为一个进程,本身不能动
    同一时间点内一个cpu只能有一个线程起着
2.新建线程时,推荐使用implements runnable,方便拓展,且只需要重写run方法
3.thread.start并未开始执行线程,只是进入就绪状态,等待cpu调度
4.线程控制基本方法:
    isAlive():判断线程是否还未终止
    getPriority():获得线程的优先级数值,线程的优先级越高,执行的时间片就越长,优先级由1到10,默认为5
    setPriority():设置线程的优先级数值
    Thread.sleep():将当前线程睡眠指定毫秒数
    join():调用某线程的该方法,将当前线程与该线程合并,即等待该线程结束,再恢复当前线程的运行
    yield():让出cpu,当前线程进入就绪队列等待调度
    wait():当前线程进入对象的wait pool
    notify()/notifyAll():唤醒对象的wait pool中的一个/所有等待线程
5.wait()和sleep()区别:wait()是Object类方法,sleep()是Thread类方法;
                                             wait()时其他线程可以访问锁定对象,sleep()时其他线程不能访问锁定对象
5.线程同步:访问同一份资源的多个线程之间进行协调
6.生成者消费者例子
public class ProductConsumer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ProductConsumer productConsumer = new ProductConsumer();
        Synstack synstack = productConsumer.new Synstack();
        Producer producer = productConsumer.new Producer(synstack);
        Consumer consumer = productConsumer.new Consumer(synstack);
        new Thread(producer).start();
        new Thread(consumer).start();

    }

    class Wotou{
        int id;
        public Wotou(int id) {
            this.id = id;
        }
        
        public String toString()
        {
            return "wotou:"+id;
        }
    }
    
    class Synstack{
        Wotou[] ssWotous = new Wotou[6];
        int index = 0;
        
        public synchronized void push(Wotou wotou)
        {
            if (index == ssWotous.length) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            ssWotous[index] = wotou;
            index++;
        }
        
        public synchronized Wotou pop()
        {
            if (index == 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            index--;
            return ssWotous[index];
        }
    }
    
    class Producer implements Runnable{
        Synstack synstack = null;
        
        public Producer(Synstack synstack)
        {
            this.synstack = synstack;
        }
        
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                Wotou wotou = new Wotou(i);
                synstack.push(wotou);
                System.out.println("produce:"+wotou);
            }
            
        }
        
    }
    
    class Consumer implements Runnable{
        Synstack synstack = null;
        
        public Consumer(Synstack synstack)
        {
            this.synstack = synstack;
        }
        
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                Wotou wotou = synstack.pop();
                System.out.println("consume:"+wotou);
            }
        }
        
    }
}

原创粉丝点击