目录
一、API
一、JoinDemo
public class JoinDemo {
public static void main(String[] args) {
RunDemo runDemo=new RunDemo();
Thread thread=new Thread(runDemo);
thread.start();
for (int i = 0; i <10 ; i++) {
if (i==3){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"-----"+i);
}
}
}
二、RunDemo
Runnable 的使用 必须对此进行重写
public class RunDemo implements Runnable {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"----"+i);
}
}
}
三、SleepDemo
睡眠规定的时间,让出cpu片资源
public class SleepDemo {
public static void main(String[] args) {
RunDemo runDemo=new RunDemo();
Thread thread=new Thread(runDemo);
thread.start();
for (int i = 0; i <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"-----"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
四、StopDemo
较为粗暴 直接停止
public class StopDemo {
public static void main(String[] args) {
RunDemo runDemo=new RunDemo();
Thread thread=new Thread(runDemo);
thread.start();
for (int i = 0; i <10 ; i++) {
if (i==2){
//让Thread停止
thread.stop();
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-----"+i);
}
}
}
五、ThreadApiDemo
public class ThreadApiDemo {
public static void main(String[] args) {
//获取当前线程的实例
Thread thread=Thread.currentThread();
//获取当前线程的名称
System.out.println(thread.getName());
//获取线程的优先级,一般情况优先级是0~10之间的整数,默认优先级是5,特殊情况下也可以是0~100之间的整数
/*
* 作用:
* 数值月打代表优先级越高,但是优先级越高并不一定意味这它会被先执行,二是说优先级高的会执行的效率更大一些,仅此而已*/
System.out.println();
}
}
六、 YieldDemo
礼让cpu资源
public class YieldDemo {
public static void main(String[] args) {
RunDemo runDemo=new RunDemo();
Thread thread=new Thread(runDemo);
thread.start();
for (int i = 0; i <10 ; i++) {
if (i==6){
//线程礼让一次,进入就绪状态,如果就绪队列中没有其他线程或者其他线程没有它自己抢占cpu资源快的话,那么它会快读的抢占CPU资源进行运行状态;
Thread.yield();
}
System.out.println(Thread.currentThread().getName()+"-----"+i);
}
}
}
二、producerAndConsumer
1.Consumer
package com.tensent.class4.producerAndConsumer;
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println("消费者消费了========="+goods.getBrand()+"======="+goods.getName());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2. Goods
package com.tensent.class4.producerAndConsumer;
/*
* 生产者和消费这共存的资源:商品*/
public class Goods {
private String brand;
private String name;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3. Producer
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
//交替生产哇哈哈矿泉水和旺仔小馒头
if (i % 2 == 0) {
goods.setBrand("哇哈哈");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
goods.setName("矿泉水");
}else{
goods.setBrand("旺仔");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
goods.setName("小馒头");
}
System.out.println("生产者生产了----"+goods.getBrand()+"----"+goods.getName());
}
}
}
4.Test
* 1.生产者未生产 ,消费者已消费
* 2.连续生产 连续消费
* 3.两个属性的名称不能正常对应
*
* 加锁!
* */
public class Test1 {
public static void main(String[] args) {
Goods goods=new Goods();
Producer producer=new Producer(goods);
Consumer consumer=new Consumer(goods);
Thread thread=new Thread(producer);
Thread thread1=new Thread(consumer);
thread.start();
thread1.start();
}
}
三、producerAndConsumer
1.Consumer
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
goods.get();
}
}
}
2. Goods
/*
* 生产者和消费这共存的资源:商品*/
public class Goods {
private String brand;
private String name;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//生产商品的方法
public synchronized void set(String brand,String name){
this.setBrand(brand);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setName(name);
System.out.println("生产者生产了----"+this.getBrand()+"----"+this.getName());
}
public synchronized void get(){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者消费了========="+this.getBrand()+"======="+this.getName());
}
}
3. Producer
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
//交替生产哇哈哈矿泉水和旺仔小馒头
if (i % 2 == 0) {
goods.set("哇哈哈","矿泉水");
}else{
goods.set("旺仔","小馒头");
}
}
}
}
4.Test
/*
* 出现问题
* 1.生产者未生产 ,消费者已消费
* 2.连续生产 连续消费
* 3.两个属性的名称不能正常对应
*
* 加锁! 解决;3
* */
public class Test1 {
public static void main(String[] args) {
Goods goods=new Goods();
Producer producer=new Producer(goods);
Consumer consumer=new Consumer(goods);
Thread thread=new Thread(producer);
Thread thread1=new Thread(consumer);
thread.start();
thread1.start();
}
}
四、producerAndConsumer
1.Consumer
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
goods.get();
}
}
}
2. Goods
/*
* 生产者和消费这共存的资源:商品*/
public class Goods {
private String brand;
private String name;
private boolean isFull;
//定义一个标志位,表示共享区域中是否存在商品
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//生产商品的方法
public synchronized void set(String brand,String name){
/*
* 如果生产者线程获取对应的cpu,得到了执行机会,此时先判断共享区域中是否存在商品,如果已经存在了,那么当前
* 生产者线程进入阻塞状态,等待消费者来消费,反之如果共享区域中不存在商品,那么生产商品就可以了。*/
if(isFull){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setName(name);
System.out.println("生产者生产了----"+this.getBrand()+"----"+this.getName());
//如果程序能执行到此处,说明已经生产商品完毕了,此时需要标志位为ture
isFull=true;
//唤醒消费者来消费
notify();
}
public synchronized void get(){
/*
* 如果消费者线程获取到对应的cpu时间片,获取到了执行机会了,此时先判断共享区域中是否存在商品,如果不存在
* 商品,当前正在执行的消费者线程进入阻塞状态,等待生产者生产商品,反之如果已经存在商品,那么消费者直接消费就可以了*/
if (!isFull){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者消费了========="+this.getBrand()+"======="+this.getName());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//如果程序运行到此处,说明消费者已经消费完毕,需要修改标志位为false
isFull=false;
//唤醒消费者前来生产
notify();
}
}
3. Producer
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
//交替生产哇哈哈矿泉水和旺仔小馒头
if (i % 2 == 0) {
goods.set("哇哈哈","矿泉水");
}else{
goods.set("旺仔","小馒头");
}
}
}
}
4.Test
public class Test1 {
public static void main(String[] args) {
Goods goods=new Goods();
Producer producer=new Producer(goods);
Consumer consumer=new Consumer(goods);
Thread thread=new Thread(producer);
Thread thread1=new Thread(consumer);
thread.start();
thread1.start();
}
}
五、producerAndConsumer
1.Consumer
public class ConsumerQueue implements Runnable {
private BlockingQueue<Goods> blockingQueue;
public ConsumerQueue(BlockingQueue<Goods> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Goods goods=null;
try {
goods = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者消费了商品======="+goods.getBrand()+"======"+goods.getName());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2. Goods
public class Goods {
private String brand;
private String name;
public Goods(String brand, String name) {
this.brand = brand;
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3. Producer
public class ProducerQueue implements Runnable {
private BlockingQueue<Goods> blockingQueue;
public ProducerQueue(BlockingQueue<Goods> blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
Goods goods=null;
if (i%2==0){
goods=new Goods("哇哈哈","矿泉水");
}else{
goods=new Goods("旺仔","小馒头");
}
System.out.println("生产者生产了商品----"+goods.getBrand()+"----"+goods.getName());
//把生产者生产的商品放入到队列中储存
try {
blockingQueue.put(goods);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
4.Test
public class Test1 {
public static void main(String[] args) {
BlockingQueue<Goods>blockingQueue=new ArrayBlockingQueue<Goods>(5);
ProducerQueue producerQueue=new ProducerQueue(blockingQueue);
ConsumerQueue consumerQueue=new ConsumerQueue(blockingQueue);
new Thread(producerQueue).start();
new Thread(consumerQueue).start();
}
}










