线程

1.线程的创建方法1

public class Demo1Thread  {
    public static void main(String[] args) {
        //匿名内部类方法
        /*new Thread(){
            @Override
            public void run() {
                for(int i=0;i<10;i++) {
                    System.out.println("锄禾日当午" + i);
                }
            }
        }*/

        myThread mt = new myThread();
        mt.start();
        for(int i=0;i<10;i++){
            System.out.println("清明上河图"+i);
        }

    }

}
class myThread extends Thread{
    /**
     * run就是线程要执行的任务方法
     */
    @Override
    public void run() {
        //这里的代码 就是一条执行路径
        //这个路径的触发方式,不是调用run方法,而是通过thread对象的start 方法来启动
        for(int i=0;i<10;i++){
            System.out.println("锄禾日当午"+i);
        }

    }
}

run函数时要执行的任务

2.线程创建方法2

public class Demo2Runnable {
    public static void main(String[] args) {
        //创建一个任务对象
        MyRunnable0 mr = new MyRunnable0();
        //创建一个线程,并为其分配一个任务
        Thread t = new Thread();
        t.start();
        for(int i=0;i<10;i++){
            System.out.println("清明上河图"+i);
        }
    }
}
class MyRunnable0 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("清明上河图" + i);
        }
    }
}

3.获取当前线程的名字

public class Demo3GetName {
    public static void main(String[] args) {
        //current.Thread 当前执行线程
        System.out.println(Thread.currentThread().getName());
        new Thread(new MyRunnable2(),"锄禾日当午").start();
        new Thread(new MyRunnable2()).start();
        new Thread(new MyRunnable2()).start();
        new Thread(new MyRunnable2()).start();

    }
}
class MyRunnable2 implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

4.程序休眠

public class Demo4Sleep {
    public static void main(String[] args) throws InterruptedException {
        // thread 的Sleep方法 可以让程序休眠指定时间
        for(int i=0;i<10;i++){
            System.out.println(i);
            Thread.sleep(1000);
        }
    }
}

可以让程序休眠指定时间
5.线程的中断

public class Demo5zhongduan {
    /**
     * 线程的中断
     * @param args
     */
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable3());
        t1.start();
        for(int i=1;i<=5;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //给t1 线程添加中断标记
        t1.interrupt();

    }
}
class MyRunnable3 implements Runnable{
    @Override
    public void run() {
        for(int i=1;i<=10;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                  return;       //发现中断标记后,结束线程
//                e.printStackTrace();
            }
        }
    }
}

当主线执行完毕后,子线程读到标记后结束线程。
6.守护线程

public class Demo6Daemon {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable4());
        //设置为守护线程,用户线程全部结束后。守护线程自动结束
        t1.setDaemon(true);
        t1.start();
        for(int i=1;i<=5;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }



    }
}
class MyRunnable4 implements Runnable{
    @Override
    public void run() {
        for(int i=1;i<=10;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
    }
}

用户线程全部结束后。守护线程自动结束
7.多线程不安全案例

public class Demo7ThreatNotSafe {
    public static void main(String[] args) {

            Runnable run = new Tickets2();
            new Thread(run).start();
            new Thread(run).start();
            new Thread(run).start();

    }
}

class Tickets2 implements Runnable {
        private int count = 10;

        @Override
        public void run() {
            while (count > 0) {
                System.out.println("准备出票");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
                System.out.println("出票成功,余票为:" + count);
            }
        }
    }

运行截图:
票数出现了负数
出现了负数,线程不安全。

8.解决线程不安全问题`

public class Demo8SolveThreatNotSafe {
    public static void main(String[] args) {
        //线程不安全
        Runnable run = new Tickets();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();

    }
}
class Tickets implements Runnable{
    private int count = 10;
    //锁一定要放在run方法前 线程得被一把锁 锁起来
    Object o = new Object();

    @Override
    public void run() {

        while(true) {
            synchronized (o) {
                if(count>0){
                    System.out.println("准备出票");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count--;
                    System.out.println(Thread.currentThread().getName()+"出票成功,余票为:"+count);
                }
                else{
                    break;
                }

            }
        }

    }
}

9.方法2

public class Demo9Method2 {
    public static void main(String[] args) {
        //三个线程执行一个run任务
        Runnable run = new Tickets3();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();

    }
}

class Tickets3 implements Runnable {
    private int count = 10;

    @Override
    public void run() {
        while(true) {
            boolean flag = sale();
            if(!flag){
                break;
            }
        }

    }

    /**
     * 此方法锁的是this
     * @return  同步代码块和同步方法都是隐式锁
     */
    public synchronized boolean sale(){
        if(count>0){
            System.out.println("准备出票");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count--;
            System.out.println(Thread.currentThread().getName()+"出票成功,余票为:"+count);
            return true;
        }
        return false;

    }
}

10.方法3

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

public class Demo10Method3 {
    public static void main(String[] args) {

        Runnable run = new Tickets4();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();

    }
}
class Tickets4 implements Runnable{
    private int count = 10;
    //显示锁       fair参数为true时为公平锁
    private Lock l = new ReentrantLock(true);

    @Override
    public void run() {

        while(true) {
            l.lock();
            if(count>0){
                System.out.println("准备出票");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
                System.out.println(Thread.currentThread().getName()+"出票成功,余票为:"+count);
            }
            else{
                break;
            }
            l.unlock();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螃蟹剥它的壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值