We believe that writing is about content, about what you want to say – not about fancy formatting.
我们坚信写作写的是内容,所思所想,而不是花样格式。
— Ulysses for Mac
进程:
- 运行时(runtime)应用程序
- 进程之间的内存不是共享的(独占)
- 进程之间通信用socket套接字
多线程:
- 进程内并发执行的代码段
- 线程之间共享内存
- 主要创建灵活响应的桌面程序
- 每个运行着的线程对应一个stack
- 应用程序至少有一个线程——主线程
- 涉及并发就有多线程
Thread.yield:
yield
(放弃cpu调用权,谦逊,瞬时动作)
Paste_Image.pngjoin
daemon 守护
Thread.setDaemon(true);
线程间通信,共享资源问题
- 加锁—>由并行改为串行,防止并发访问
- 参照物,锁旗标
- 锁定权
对锁对象同步:
public class Ptest {
public static void main(String[] args) {
Salar s1 = new Salar("s1");
Salar s2 = new Salar("s2");
s1.start();
s2.start();
}
}
class Salar extends Thread{
static int tickets = 100;
// 锁对象(锁旗标)
static Object lock = new Object();
private String name;
public Salar(String name){
this.name = name;
}
public void run(){
while(tickets>0){
int temp = tickets;
System.out.println(name+" : "+temp);
tickets = tickets - 1;
}
}
public int getTicket(){
synchronized (lock) {
int t = tickets;
tickets = tickets - 1;
return t;
}
}
}
Paste_Image.png
多线程加了同步 会降低代码性能
同步代码块执行期间 线程始终持有对象的监控权,其他线程处于阻塞状态
public class Ptest {
public static void main(String[] args) {
TicketPool pool = new TicketPool();
Salar s1 = new Salar("s1",pool);
Salar s2 = new Salar("s2",pool);
Salar s3 = new Salar("s3",pool);
Salar s4 = new Salar("s4",pool);
s1.start();
s2.start();
s3.start();
s4.start();
}
}
class Salar extends Thread{
private String name;
private TicketPool pool;
public Salar(String name, TicketPool pool){
this.name = name;
this.pool = pool;
}
public void run(){
while(true){
int no = pool.getTicket();
if(no == 0){
return;
}else{
System.out.println(name+" : "+no);
Thread.yield();
}
}
}
}
// 票池
class TicketPool{
private int tickets = 10;
public int getTicket(){
// 同步代码块 以票持本身作为锁旗标
synchronized (this) {
int temp = tickets;
tickets = tickets - 1;
return temp > 0 ? temp : 0;
}
}
}
同步方法是以当前所在对象做锁旗标:
synchronized(this) == 同步方法
synchronized 加在方法上:
public class Ptest {
public static void main(String[] args) {
TicketPool pool = new TicketPool();
Salar s1 = new Salar("s1",pool);
Salar s2 = new Salar("s2",pool);
Salar s3 = new Salar("s3",pool);
Salar s4 = new Salar("s4",pool);
s1.start();
s2.start();
s3.start();
s4.start();
}
}
class Salar extends Thread{
private String name;
private TicketPool pool;
public Salar(String name, TicketPool pool){
this.name = name;
this.pool = pool;
}
public void run(){
while(true){
int no = pool.getTicket();
if(no == 0){
return;
}else{
System.out.println(name+" : "+no);
Thread.yield();
}
}
}
}
// 票池
class TicketPool{
private int tickets = 10;
public synchronized int getTicket(){
int temp = tickets;
tickets = tickets - 1;
return temp > 0 ? temp : 0;
}
}
用map存放身份证信息:
/**
* List相当于人群,存储很多人(map)
* 单个map信息的key必须不同 借助List使得可以存储多个map(人)的信息
*/
List<HashMap<String, String>> list = new LinkedList<HashMap<String,String>>();
/**
* 每个map存一个人的信息
*/
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("name", "王二小");
map1.put("sex", "男");
map1.put("nation", "汉");
map1.put("birth", "1990-09-12");
map1.put("addr", "北京海淀区");
map1.put("cardid", "123");
list.add(map1);
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("name", "王二小");
map2.put("sex", "男");
map2.put("nation", "汉");
map2.put("birth", "1990-09-12");
map2.put("addr", "北京海淀区");
map2.put("cardid", "123");
list.add(map2);
for(int i=0; i<list.size(); i++){
System.out.println("第 "+i+" 个人信息:");
System.out.println(list.get(i).get("name"));
System.out.println(list.get(i).get("sex"));
System.out.println(list.get(i).get("nation"));
System.out.println(list.get(i).get("birth"));
System.out.println(list.get(i).get("addr"));
System.out.println(list.get(i).get("cardid"));
}
// 输出:
第 0 个人信息:
王二小
男
汉
1990-09-12
北京海淀区
123
第 1 个人信息:
王二小
男
汉
1990-09-12
北京海淀区
123
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
世界上所有的追求都是因为热爱
一枚爱编码 爱生活 爱分享的IT信徒
— hongXkeX
网友评论