2021-2-23
1.Properties概述
package com.wc.charstream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* @author wc
* @Date: 2021/02/23/9:39
*/
public class Property {
public static void main(String[] args) {
Properties properties=new Properties();
//增
properties.put("卷子","12");
properties.put("菊子","13");
System.out.println(properties);
//删
// properties.remove("卷子");
// System.out.println(properties);
// //改
// properties.put("橘子","12");
// System.out.println(properties);
// //查
// Object value = properties.get("橘子");
// System.out.println(value);
//遍历
//根据键查找值
Set<Object> keys = properties.keySet();
for (Object key : keys) {
Object value = properties.get(key);
System.out.println(key+"="+value);
}
System.out.println("========================");
//直接获取键值对,挨个获取键和值
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
2.properties特有方法
3.Properties与IO有关的方法(load,store)
package com.wc.charstream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
/**
* @author wc
* @Date: 2021/02/23/9:39
*/
public class Property {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("Stream\\prop.properties");
//将本地文件中的键值对数据读取到集合中
properties.load(fr);
fr.close();
System.out.println(properties);
System.out.println("---------------------");
properties.setProperty("zhangsan", "12");
properties.setProperty("lisi", "13");
properties.setProperty("wangwu", "14");
FileWriter fw = new FileWriter("Stream\\prop.properties");
properties.store(fw,null);
fw.flush();
fw.close();
System.out.println(properties);
}
}
4.多线程概述(并行和并发、进程和线程)
5.多继承的实现方式(thread,runnable,callable和future)
6.Thread方法(获取名称)
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/23/10:21
*/
public class MyThreadDemo extends Thread {
@Override
public void run() {
String name = getName();
System.out.println(name);
Thread thread = currentThread();
System.out.println(thread);
}
}
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/23/10:23
*/
public class TestDemo {
public static void main(String[] args) {
//开启线程
MyThreadDemo myThreadDemo=new MyThreadDemo();
//开启第一次线程
myThreadDemo.start();
new MyThreadDemo().start();
}
}
7.Thread的方法
8.线程安全问题
1.同步代码块
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/23/14:21
*/
public class RunnableImpl implements Runnable {
private int ticket=100;
//创建一个锁对象
Object obj=new Object();
@Override
public void run() {
//设置线程任务,卖票
//使用死循环,重复卖票的动作
while (true){
//判断票是否存在
synchronized (obj){
if (ticket>0){
System.out.println(Thread.currentThread().getName()+"-->当前正在出售第"+ticket+"张票");
ticket--;
}
}
}
}
}
2.同步方法
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/23/14:21
*/
public class RunnableImpl implements Runnable {
private int ticket=100;
//创建一个锁对象
@Override
public void run() {
//设置线程任务,卖票
//使用死循环,重复卖票的动作
while (true){
//判断票是否存在
{
method();
}
}
}
private synchronized void method() {
if (ticket>0){
System.out.println(Thread.currentThread().getName()+"-->当前正在出售第"+ticket+"张票");
ticket--;
}
}
}
9.生产者和消费者(线程了解加深,wait、notify)
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/24/9:38
*/
public class Cooker extends Thread {
private Desk desk;
public Cooker(Desk desk) {
this.desk=desk;
}
/*1.判断桌子上是否有汉堡
如果有就等待消费者吃,没有就生产
2.把汉堡放在桌子上
3.叫醒等待的消费者开吃
* */
@Override
public void run() {
//设置线程任务
while (true){
synchronized (desk.getLock()){
if (desk.getCount()==0){
//count为零时,说明没有吃汉堡需求
break;
}else {
//如果桌子上没有汉堡
if (!desk.isFlag()){
System.out.println("厨师正在生产汉堡包");
desk.setFlag(true);
desk.getLock().notifyAll();
}else {
try {
desk.getLock().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
package com.wc.thread;
import java.awt.*;
/**
* @author wc
* @Date: 2021/02/24/9:38
*/
public class Eater extends Thread {
/*消费者步骤:
1.判断桌子上是否有汉堡
2.如果有就开吃
3.如果没有就等待
4.吃完之后,桌子上汉堡就没了,
叫醒等待的生产者继续生产,
每吃一个汉堡就减少一个
* */
private Desk desk;
public Eater(Desk desk) {
this.desk = desk;
}
@Override
public void run() {
//设置线程任务
while (true) {
//synchronized锁,锁对象要唯一
synchronized (desk.getLock()) {
if (desk.getCount() == 0) {
//count为零时,说明没有吃汉堡需求
break;
} else {
//如果桌子上有汉堡
if (desk.isFlag()) {
System.out.println("正在吃汉堡");
//汉堡吃完没有了
desk.setFlag(false);
//唤醒线程,与锁有关
desk.getLock().notifyAll();
//每吃一个减少一个
desk.setCount( desk.getCount()-1);
} else {
try {
//如果没有就等待
desk.getLock().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/24/9:38
*/
public class Desk {
//定义一个标记
//如果为true,表示桌子上有汉堡,允许吃货执行
//如果为false,表示桌子上没有汉堡,允许厨师执行
//public static boolean flag=false;
private boolean flag;
//定义汉堡个数,汉堡最多生产10个
//如果数量为0,说明没有汉堡生产的需求
//public static int count=10;
private int count;
//定义一个锁对象
//线程
//public static final Object lock=new Object();
private final Object lock = new Object();
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Object getLock() {
return lock;
}
public Desk(boolean flag, int count) {
this.flag = flag;
this.count = count;
}
public Desk() {
this(false,10);
}
@Override
public String toString() {
return "Desk{" +
"flag=" + flag +
", count=" + count +
", lock=" + lock +
'}';
}
}
package com.wc.thread;
/**
* @author wc
* @Date: 2021/02/24/9:38
*/
public class ThreadTest {
public static void main(String[] args) {
Desk desk=new Desk();
Cooker cooker=new Cooker(desk);
Eater eater=new Eater(desk);
cooker.start();
eater.start();
}
}
10.阻塞队列