线程

线程:

线程就是程序执行的一条路径,一个进程中可以包含多条线程

多条线程并发执行可以提高程序的效率,可以同时完成多项工作

实现方法:

第一种:继承Thread

public class Demo1_Thread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread mt=new MyThread();
        mt.start();

        for(int i=0;i<1000;i++){
            System.out.println("bbbbbb");
        }
    }

}
class MyThread extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<1000;i++){
            System.out.println("aaaaaaaaaaaaaa");
        }
    }
}

第二种:实现Runnable接口

public class Demo2_Thread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MThread mt=new MThread();
        new Thread(mt).start();
        for(int i=0;i<1000;i++){
            System.out.println("bbb");
        }
    }

}
class MThread implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<1000;i++){
            System.out.println("aaaaaaaaaaaaaa");
        }
    }

}

线程比较常用的方法:

getName():得到当前线程的名字

sleep():等待传入的毫秒的时间之后继续执行

join():当前线程暂停,等待指定的线程执行后,当前线程再执行

currentThread():获取当前线程的对象

public class Demo3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Thread t1=new Thread(){
            public void run(){
                for(int i=0;i<10;i++){

                    System.out.println(this.getName()+":aaaaaaaaa");
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                for(int i=0;i<10;i++){
                    if(i==2){
                        try {
                            t1.join();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println(this.getName()+":bb");
                }
            }
        };
        t1.start();
        t2.start();
    }

}

线程—火车站购票

public class Test1 {
    /**
     * 模拟火车站购票
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Ticket t=new Ticket();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }

}
class Ticket implements Runnable{
    //票的总数
    private int tickets=100;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            //线程安全,同步代码块
            synchronized (Ticket.this) {
                //当tickets小于等于0就跳出while
                if(tickets<=0){
                    break;
                }
                try {
                    //睡眠10毫秒
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"这是第"+tickets--+"号票");
            }
        }
    }

}

互斥锁:防止线程并发

ReentrantLock:

lock():获取锁
unlock():释放锁

Condition:

await():处于等待状态。
signal():唤醒一个等待线程。
public class Demo4_lock {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mylock m = new Mylock();
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        m.print1();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        m.print2();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        m.print3();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

}

class Mylock {
    private int i = 1;
    private ReentrantLock rtl = new ReentrantLock();
    private Condition cd1 = rtl.newCondition();
    private Condition cd2 = rtl.newCondition();
    private Condition cd3 = rtl.newCondition();

    public void print1() throws InterruptedException {
        rtl.lock();
        if (i != 1) {
            cd1.await();
        }
        System.out.print("你");
        System.out.print("好");
        System.out.print("呀");
        System.out.println();
        i = 2;
        cd2.signal();
        rtl.unlock();
    }

    public void print2() throws InterruptedException {
        rtl.lock();
        if (i != 2) {
            cd2.await();
        }
        System.out.print("臭");
        System.out.print("宁");
        System.out.print("宁");
        System.out.println();
        i = 3;
        cd3.signal();
        rtl.unlock();

    }

    public void print3() throws InterruptedException {
        rtl.lock();
        if (i != 3) {
            cd3.await();
        }
        System.out.print("不");
        System.out.print("知");
        System.out.print("道");
        System.out.println();
        i = 1;
        cd1.signal();
        rtl.unlock();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值