前言
本期任务:毕向东老师Java视频教程学习笔记(共计25天)
原视频链接:黑马程序员_毕向东_Java基础视频教程day01:编写HelloWorld程序day02:操作符与条件选择语句day03:循环语句与函数day04:数组day07:继承、抽象类与接口day08:多态day09:异常处理day11:多线程day12:线程安全与同步机制day13:String类day14:集合(ArrayList,LinkedList,HashSet)day15:集合(TreeSet)和泛型)day16:集合(HashMap、TreeMap)day17:集合框架的工具类(Arrays、Collections)day18:IO流(字符流读写)day19:IO流(字节流、转换流读写)day20:IO流(File对象)
代码
package day12
;
class Resourse1 {
String name
;
String sex
;
boolean flag
= false;
}
class Input implements Runnable {
private Resourse1 r
;
Input(Resourse1 r
) {
this.r
= r
;
}
public void run() {
int x
= 0;
while (true) {
synchronized (r
) {
if (r
.flag
)
try {
r
.wait();
} catch (Exception e
) {
}
if (x
== 0) {
r
.name
= "mike";
r
.sex
= "man";
} else {
r
.name
= "丽丽";
r
.sex
= "女";
}
r
.flag
= true;
r
.notify();
x
= (x
+ 1) % 2;
}
}
}
}
class Output implements Runnable {
private Resourse1 r
;
Output(Resourse1 r
) {
this.r
= r
;
}
public void run() {
while (true) {
synchronized (r
) {
if (!r
.flag
)
try {
r
.wait();
} catch (Exception e
) {
}
r
.flag
= false;
r
.notify();
if ((r
.name
== "mike" && r
.sex
== "女") || (r
.name
== "丽丽" && r
.sex
== "man"))
System
.out
.println(r
.name
+ "......" + r
.sex
);
}
}
}
}
public class InputOutputDemo {
public static void main(String
[] args
) {
Resourse1 r
= new Resourse1();
new Thread(new Input(r
)).start();
new Thread(new Output(r
)).start();
}
}
package day12
;
class Resourse2 {
String name
;
String sex
;
private int x
;
boolean flag
= false;
public synchronized void set() {
if (flag
)
try {
wait();
} catch (Exception e
) {
}
if (x
== 0) {
name
= "mike";
sex
= "man";
} else {
name
= "丽丽";
sex
= "女";
}
flag
= true;
x
= (x
+ 1) % 2;
this.notify();
}
public synchronized void out() {
if (!flag
)
try {
wait();
} catch (Exception e
) {
}
flag
= false;
this.notify();
if ((name
== "mike" && sex
== "女") || (name
== "丽丽" && sex
== "man"))
System
.out
.println(name
+ "......" + sex
);
}
}
class Input2 implements Runnable {
private Resourse2 r
;
Input2(Resourse2 r
) {
this.r
= r
;
}
public void run() {
while (true) {
{
r
.set();
}
}
}
}
class Output2 implements Runnable {
private Resourse2 r
;
Output2(Resourse2 r
) {
this.r
= r
;
}
public void run() {
while (true) {
r
.out();
}
}
}
public class InputOutputOptimize {
public static void main(String
[] args
) {
Resourse2 r
= new Resourse2();
new Thread(new Input2(r
)).start();
new Thread(new Output2(r
)).start();
}
}
package day12
;
import java
.util
.concurrent
.locks
.*
;
class Resourse {
private String name
;
private int count
= 1;
private boolean flag
= false;
private Lock lock
= new ReentrantLock();
private Condition condition_pro
= lock
.newCondition();
private Condition condition_con
= lock
.newCondition();
public void set(String name
) throws InterruptedException
{
lock
.lock();
try {
while (flag
)
condition_pro
.await();
this.name
= name
+ "......" + count
++;
System
.out
.println(Thread
.currentThread().getName() + "生产者" + this.name
);
flag
= true;
condition_con
.signal();
} finally {
lock
.unlock();
}
}
public void out() throws InterruptedException
{
lock
.lock();
try {
while (!flag
)
condition_con
.await();
System
.out
.println(Thread
.currentThread().getName() + "消费者............" + this.name
);
flag
= false;
condition_pro
.signal();
} finally {
lock
.unlock();
}
}
}
class Producer implements Runnable {
private Resourse res
;
Producer(Resourse res
) {
this.res
= res
;
}
public void run() {
while (true) {
try {
res
.set("商品");
} catch (InterruptedException e
) {
}
}
}
}
class Consumer implements Runnable {
private Resourse res
;
Consumer(Resourse res
) {
this.res
= res
;
}
public void run() {
while (true) {
try {
res
.out();
} catch (InterruptedException e
) {
}
}
}
}
public class ProducerConsumerDemo {
public static void main(String
[] args
) {
Resourse res
= new Resourse();
Producer p1
= new Producer(res
);
Producer p2
= new Producer(res
);
Consumer c1
= new Consumer(res
);
Consumer c2
= new Consumer(res
);
new Thread(p1
).start();
new Thread(p2
).start();
new Thread(c1
).start();
new Thread(c2
).start();
}
}
package day12
;
class Demo implements Runnable {
private int size
= 1000;
Demo(int size
) {
this.size
= size
;
}
Demo() {
}
public void run() {
for (int x
= 0; x
< size
; x
++) {
System
.out
.println(Thread
.currentThread().getName() + "......" + x
);
Thread
.yield();
}
}
}
public class JoinDemo {
public static void main(String
[] args
) throws Exception
{
Thread t1
= new Thread(new Demo(1000));
Thread t2
= new Thread(new Demo());
t1
.start();
t2
.start();
System
.out
.println("over");
}
}