![](https://i-blog.csdnimg.cn/blog_migrate/c9f846c92bbf9cb0c5c872e1d3761da6.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/8eade282b0a5f8fd073aecea38dffaaf.webp?x-image-process=image/format,png)
sleep()和wait()有什么区别
![](https://i-blog.csdnimg.cn/blog_migrate/525f66abaf6074be63a412589a12ef98.webp?x-image-process=image/format,png)
同步和异步有何异同,在什么情况下分别使用他们
![](https://i-blog.csdnimg.cn/blog_migrate/68f14e09a9754b8b827c54e09bfd0392.webp?x-image-process=image/format,png)
多线程有几种实现方法?同步有几种实现方法?
![](https://i-blog.csdnimg.cn/blog_migrate/20609ba166172946205b622640096960.webp?x-image-process=image/format,png)
多线程实现方式
继承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();
}
}
}
线程的基本概念、线程的基本状态以及状态之间的关系
![](https://i-blog.csdnimg.cn/blog_migrate/6431109d4acf73a7fca73e5923dbd495.webp?x-image-process=image/format,png)
线程状态之间的关系:
![](https://i-blog.csdnimg.cn/blog_migrate/af9a1d57b1591bf04b13f122342004a3.webp?x-image-process=image/format,png)