线程的记录

public class Rabbit extends Thread {

    @Override
    public  void  run() {
        for(int i=0;i<1000;i++) {
            System.out.println("兔子跑---"+i+"步--");
        }

    }
}
public class DemoApplicationTests {

    public static void main(String[] args) {
        Rabbit rabbit = new Rabbit();
        rabbit.start();
        for(int i=0;i<1000;i++) {
            System.out.println("main跑---"+i+"步--");
        }
    }


}

main跑—962步–
兔子跑—39步–
main跑—963步–
兔子跑—40步–
main跑—964步–
兔子跑—41步–
main跑—965步–
兔子跑—42步–
main跑—966步–
兔子跑—43步–
main跑—967步–
兔子跑—44步–
main跑—968步–


因为java是单继承多实现,所以用Thread就占用了继承位置,可以通过实现runnable接口来实现进程

public class DemoApplicationTests {

    public static void main(String[] args) {
        Rabbit rabbit = new Rabbit();
        //静态代理
        Thread proxy = new Thread(rabbit);
        proxy.start();
        for(int i=0;i<1000;i++) {
            System.out.println("main跑---"+i+"步--");
        }
    }
public class Rabbit implements Runnable {

    @Override
    public  void  run() {
        for(int i=0;i<1000;i++) {
            System.out.println("兔子跑---"+i+"步--");
        }

    }

main跑—965步–
main跑—966步–
main跑—967步–
兔子跑—594步–
main跑—968步–
兔子跑—595步–
main跑—969步–


public class Web12306 implements Runnable {
    private int ticket=1000;
    @Override
    public void run() {
        while (true) {
            if(ticket<0) {
                System.out.println("票卖完了");
                break;
            }
            System.out.println(Thread.currentThread().getName()+"抢到了"+ticket--+"号的票");

        }
    }
}
public class DemoApplicationTests {

    public static void main(String[] args) {
        Web12306 web = new Web12306();
        Thread t1 = new Thread(web,"农民工");
        Thread t2 = new Thread(web,"码农");
        Thread t3 = new Thread(web,"土豪");
        t1.start();
        t2.start();
        t3.start();
    }

农民工抢到了10号的票
土豪抢到了11号的票
农民工抢到了8号的票
码农抢到了9号的票
农民工抢到了6号的票
土豪抢到了7号的票
农民工抢到了4号的票
码农抢到了5号的票
农民工抢到了2号的票
土豪抢到了3号的票
农民工抢到了0号的票
票卖完了
码农抢到了1号的票
票卖完了
票卖完了


callable可以实现有返回值,可以抛异常的线程。用起来比较繁琐

这里写图片描述

新生:new出来,通过start()来进入就绪状态

就绪状态:具备运行条件,如果cpu调用,就可以进入运行状态

运行状态:在CPU中运行

阻塞:永远不会进入CPU,除非解除阻塞

死亡:执行完毕或者destroy


public static void main (String[] args) {
        Rabbit rabbit = new Rabbit();
        rabbit.start();
        for(int i=0;i<1000;i++) {
            System.out.println("mian-------------->"+i);
            if(300 == i){

                try{
                //等rabbit这个线程执行完毕再执行别人
                    rabbit.join();
                //暂停线程,但是说不定下一秒还是调用它
                //Thread.yield();在哪个线程里调用这个方法就暂停谁
                }catch (Exception e){

                }

            }
        }
    }
public class Rabbit extends Thread {

    @Override
    public  void  run() {
        for(int i=0;i<1000;i++) {
            System.out.println("兔子跑---"+i+"步--");
        }

    }
}

mian————–>297
mian————–>298
mian————–>299
mian————–>300
兔子跑—0步–
兔子跑—1步–
兔子跑—2步–
兔子跑—3步–
兔子跑—4步–
兔子跑—5步–
兔子跑—6步–
兔子跑—7步–
兔子跑—8步–
兔子跑—9步–
兔子跑—10步–
兔子跑—11步–
兔子跑—12步–
兔子跑—13步–


sleep


Thread t;
t.setName()
t.getName()//获取线程名称

Thread.currentThread().getName()//获取当前线程名称

boolean t.isAlive();//返回是否存活

优先级
/**
     * The minimum priority that a thread can have.
     */
 public final static int MIN_PRIORITY = 1;

   /**
     * The default priority that is assigned to a thread.
     * 默认是5
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10;
setPriority(int newPriority) 
          更改线程的优先级。
t1.set(MIN_PRIORITY)
t2.set(MAX_PRIORITY);

//t2执行的概率变高

前面卖票线程不安全
土豪抢到了8号的票
农民工抢到了8号的票
这是不加 public synchronized void test2()

synchronizedthis) {
//同步代码块
}

单例设计模式会出现线程问题

syncronized Student getInstance();

class Student{
static Student getInstance(){
    //没有对象,只能锁类的字节码
    syncronized(Student.class){



}

}

死锁
过多的同步会造成死锁

生产者和消费者问题:解决死锁

void wait()//释放锁,等待
sleep()抱着锁睡觉
notify唤醒
notifyAll唤醒所有
生产双:88
我在吃:蛋糕双双:88
生产单:89
我在吃:蛋糕单单:89
生产双:90
生产单:91
我在吃:蛋糕单单:91
我在吃:蛋糕单单:91
public class cake {

     String cake;

    //生产蛋糕
    public void product(String cake) {
        this.cake=cake;
    }

    //消费蛋糕
    public void consume() {
        System.out.println("我在吃:"+cake);
    }
}
public class Producter implements Runnable {
    private cake c;

    public Producter(cake c) {
        this.c = c;
    }

    @Override
    public void run() {
        for (int i=1;i<200;i++) {
            if(0 == i%2) {
                c.product("蛋糕双双:"+i);
                System.out.println("生产双:"+i);
            }else {
                c.product("蛋糕单单:"+i);
                System.out.println("生产单:"+i);
            }
            try {
                Thread.sleep(100);
            }catch (Exception e) {
            }
        }

    }
public class Consumer implements Runnable{
    private cake c;

    public Consumer(cake c) {
        this.c = c;
    }

    @Override
    public void run() {
        for (int i=1;i<200;i++) {
            c.consume();
            try {
                Thread.sleep(100);
            }catch (Exception e) {
            }

        }



    }
}
public static void main (String[] args) throws Exception{
        cake c = new cake();
        Producter producter = new Producter(c);
        Consumer consumer = new Consumer(c);

        new Thread(producter).start();
        new Thread(consumer).start();


    }

完全体
public class cake {

     String cake;
     //True:我做蛋糕,等着别吃,做完告诉你
    //false:我吃,等着别做,吃完告诉你
     boolean flag=true;

    //生产蛋糕
    public synchronized void product(String cake) {
        //生产者等待,因为消费者在消费
        if(!flag) {
            try {
                this.wait();
            }catch (Exception e){
            }
        }
        this.cake=cake;
        System.out.println("生产");
        //通知消费者苏醒
        this.notify();
        //让生产者沉睡
        this.flag=false;
    }

    //消费蛋糕
    public synchronized void consume() {
        if(flag) {
            try {
                this.wait();
            }catch (Exception e){
            }
        }
        System.out.println("我在吃:"+cake);
        this.notify();
        this.flag=true;
    }
}
public class Producter implements Runnable {
    private cake c;

    public Producter(cake c) {
        this.c = c;
    }

    @Override
    public void run() {
        for (int i=1;i<200;i++) {
            if(0 == i%2) {
                c.product("蛋糕双双:"+i);

            }else {
                c.product("蛋糕单单:"+i);

            }
        }

    }
}
public class Consumer implements Runnable{
    private cake c;

    public Consumer(cake c) {
        this.c = c;
    }

    @Override
    public void run() {
        for (int i=1;i<200;i++) {
            c.consume();
        }
    }
}
public static void main (String[] args) throws Exception{
        cake c = new cake();
        Producter producter = new Producter(c);
        Consumer consumer = new Consumer(c);
        new Thread(producter).start();
        new Thread(consumer).start();
    }

我在吃:蛋糕双双:196
生产
我在吃:蛋糕单单:197
生产
我在吃:蛋糕双双:198
生产
我在吃:蛋糕单单:199


public static void main (String[] args) throws Exception{
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           @Override
                           public void run() {
                               System.out.println("这是定时器,程序运行的当前时间一秒后执行,间隔2秒执行一次");
                           }
                       }

                , new Date(System.currentTimeMillis() + 1000), 2000);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值