1、 使用synchronized 、wait() 、notifyAll()方法实现 生产者消费者模型
public class Demo04 {
private static int count =0;
private static final int FULL=10;
private static final String LOCK = "lock";
class producer implements Runnable{
private String name;
public producer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
while (true){
synchronized (LOCK){
if(FULL==10){
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
System.out.println(this.getName()+"生产,目前共有商品的个数:"+count);
LOCK.notifyAll();
}
}
}
}
class customer implements Runnable{
private String name;
public customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
while (true){
synchronized (LOCK){
if(FULL==0){
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
LOCK.notifyAll();
System.out.println(this.getName()+"消费,目前共有商品的个数:"+count);
}
}
}
}
public static void main(String[] args) {
Demo04 demo04 = new Demo04();
customer c1 = demo04.new customer("消费者");
producer p1 = demo04.new producer("生产者");
Thread t1 = new Thread(c1);
Thread t2 = new Thread(p1);
t1.start();
t2.start();
}
}
2、 使用3种不同的方式创建线程
//继承Thread类创建线程
public class Demo01 {
public static void main(String[] args) {
myThread m1 = new myThread();
myThread m2 = new myThread();
m1.start();
m2.start();
}
}
class myThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+":"+i);
}
}
}
//实现Runable类创建线程
public class Demo02 {
public static void main(String[] args) {
myThread2 m1 = new myThread2("张三");
myThread2 m2 = new myThread2("李四");
Thread t1 = new Thread(m1);
Thread t2 = new Thread(m2);
t1.start();
t2.start();
}
}
class myThread2 implements Runnable{
private String name;
public myThread2(String name) {
this.name = name;
}
public myThread2() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+":"+i);
}
}
}
//实现Callable类创建线程
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Demo03 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
myThread3 m1 = new myThread3("张三");
myThread3 m2 = new myThread3("李四");
FutureTask<Integer> task1 = new FutureTask<>(m1);
FutureTask<Integer> task2 = new FutureTask<>(m2);
Thread t1 = new Thread(task1);
Thread t2 = new Thread(task2);
t1.start();
t2.start();
task1.get();
task2.get();
}
}
class myThread3 implements Callable<Integer>{
private String name;
public myThread3(String name) {
this.name = name;
}
public myThread3() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Integer call() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName()+":"+i);
}
return 1;
}
3、 测试3种不同线程池,体会下区别
4、 默写出线程的线程的生命周期,以及不同状态做什么事情?
1.新建状态:使用new关键字进入的状态-不执行
2.就绪状态:经过start方法进入的状态,等待系统调度然后进入运行状态 -不执行
3.堵塞状态:当线程的执行过程中发生阻塞才进入的状态,如sleep方法
阻塞状态解除后进入就绪状态
4.运行状态:经过线程调度器调用后进入的状态 -执行
当线程的时间片执行完毕后没有完成任务,则回到就绪状态
当在线程的时间片时间以内完成任务,则进入消亡状态
5.消亡状态:当线程的任务执行完毕后进入的状态,此时线程已经终止