Java面试题-线程







sleep()和wait()有什么区别


同步和异步有何异同,在什么情况下分别使用他们


多线程有几种实现方法?同步有几种实现方法?


多线程实现方式

继承Threand类

class MusicThread extends Thread{
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println("听音乐"+ i);
        }
    }
}
public class ExtendsThreadDemo {
    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            System.out.println("打游戏"+i);
            if (i == 5) {
                MusicThread musicThread = new MusicThread();
                musicThread.start();
            }   
        }
    }
}

实现Runnable接口

class MusicRunnableImpl implements Runnable {
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println("听音乐" + i);
        }
    }
}
public class ImplementsThreadDemo {
    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            System.out.println("打游戏" + i);
            if (i == 10) {
                Runnable target = new MusicRunnableImpl();
                Thread thread  = new Thread(target);
                thread.start();
            }
        }
    }
}

同步

synchronized

class Brick implements Runnable
{
    int branckNum = 50;
    public void run() {
        for (int i = 0; i < 50; i++) {
            synchronized(this){
                 if (branckNum > 0) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "搬了第" + branckNum-- + "号砖");
                    }
            }
        }
    }   
}
public class SynchronizedBlockDemo {
    public static void main(String[] args) {
        Brick branck = new Brick();
        new Thread(brank,"toby1").start();
        new Thread(brank,"toby2").start();
        new Thread(brank,"toby3").start();
    }
}

wait和notify

public class ShareResource {
    private String name;
    private String gender;
    private boolean isEmpty = true;
    /**
     * 生产者向共享资源存储数据
     * @param name 名字
     * @param gender 性别
     */
    synchronized public void push(String name,String gender) {
        try {
            //有内容的时候,停在这里等待唤醒
            while (!isEmpty) {
                this.wait();
            }
            //设置姓名,性别
            this.name = name;
            Thread.sleep(10);
            this.gender = gender;
            //设置完了属性后,将资源设置为非空,并唤醒消费者
            //notify:执行该方法的线程唤醒在等待池中等待的任意一个线程,把线程转到锁池中等待.
            isEmpty = false;
            this.notify();
        } catch (Exception e) {
        }
    }
    /**
     * 消费者从共享资源取出数据
     */
    synchronized public void popup() {
        try {
            while (isEmpty) {
                this.wait();
            }
            Thread.sleep(10);
            //消费开始
            System.out.println("name:" + name + "-" + "gender:" + gender);
            //消费结束
            isEmpty = true;
            //唤醒一个生产者
            this.notify();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

线程的基本概念、线程的基本状态以及状态之间的关系


线程状态之间的关系:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值