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();
        }
    }
}

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


线程状态之间的关系:


你可能感兴趣的:(java,面试题,线程,面试相关)