java多线程基础

多线程基础

读书练习照猫画虎

 package Threadtest;

 import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class ThreadExerc {
public static void main(String[] args) throws Exception{
/*
* join
*/
// new JoinThread("new thread").start();
// for(int i=0; i<100; i++){
// if(i==20){
// JoinThread jt = new JoinThread("Joined Thread");
// jt.start();
// jt.join();
// }
// System.out.println(Thread.currentThread().getName()+"--"+i);
// }
/*
* sleep
*/
//testSleep(); /*
* yeild
*/
//testYeild(); /*
* 传统线程间通信
*/
// Account ac = new Account("123456", 0);
// new DrawThread("取钱者甲", ac, 1000).start();
// new DrawThread("取钱者乙", ac, 1000).start();
// new DrawThread("取钱者丙", ac, 1000).start();
// new DepositThread("存钱者A", ac, 1000).start();
// new DepositThread("存钱者B", ac, 1000).start();
// new DepositThread("存钱者C", ac, 1000).start();
//
/*
* 阻塞队列线程间通信---ArrayBlockingQueue
*/
BlockingQueue<String> bq = new ArrayBlockingQueue<>(5);
new Producer("工厂A", bq).start();
new Producer("工厂B",bq).start();
new Producer("工厂C",bq).start();
new Consumer("客户A",bq).start();
new Consumer("客户B",bq).start();
}
static void testSleep() throws Exception{
int i = 1;
while(i++<100){
System.out.println("Current Time is : "+ new Date());
Thread.sleep(10000);
} }
static void testYeild() {
class TestY extends Thread{
public TestY(String name){
super(name);
}
public void run(){
for(int i=0; i<100; i++){
System.out.println(getName() + "--" + i);
if(i==20) Thread.yield();
}
}
}
TestY t1 = new TestY("higher one");
TestY t2 = new TestY("lowwer one");
// t1.setPriority(Thread.MAX_PRIORITY);
// t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start(); }
} class JoinThread extends Thread{
public JoinThread(String name){
super(name);
}
public void run(){
for(int i=0; i<100; i++){
System.out.println(getName()+"--"+i);
}
}
} class Account{
private String accountNo;
private double ballance;
private boolean flag = false;
private final Lock lock = new ReentrantLock();
private final Condition cond = lock.newCondition();
public Account(String accountNo, double ballance) {
super();
this.accountNo = accountNo;
this.ballance = ballance;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBallance() {
return ballance;
}
// public synchronized void draw(double drawAmount){
public void draw(double drawAmount){
lock.lock();
try{
if(!flag ){
//wait();
cond.await();
}else{
System.out.println(Thread.currentThread().getName()+" 取钱: "+drawAmount);
ballance -= drawAmount;
System.out.println("余额:"+ballance);
flag = !flag;
//notifyAll();
cond.signalAll();
}
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
// public synchronized void deposit(double depositAmount){
public void deposit(double depositAmount){
lock.lock();
try{
if(flag){
// wait();
cond.await();
}else{
System.out.println(Thread.currentThread().getName()+" 存钱: "+depositAmount);
ballance += depositAmount;
System.out.println("余额:"+ballance);
flag = !flag;
// notifyAll();
cond.signalAll();
}
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
} }
class DrawThread extends Thread{
private Account ac;
private double drawAmount;
public DrawThread(String name , Account ac, double drawAmount){
super(name);
this.ac = ac;
this.drawAmount = drawAmount;
}
public void run(){
for(int i=0; i<100; i++){
ac.draw(drawAmount);
System.out.println(super.getName()+","+i);
}
}
}
class DepositThread extends Thread{
private Account ac;
private double depositAmount;
public DepositThread(String name , Account ac, double depositAmount){
super(name);
this.ac = ac;
this.depositAmount = depositAmount;
}
public void run(){
for(int i=0; i<100; i++){
ac.deposit(depositAmount);
System.out.println(super.getName()+","+i);
}
}
} //================================
class Producer extends Thread{
private BlockingQueue<String> bq; public Producer(String name, BlockingQueue<String> bq) {
super(name);
this.bq = bq;
}
public void run(){
String[] strArr = new String[]{
"java",
"Spring",
"struts",
"batis"
};
for(int i=0; i<1000; i++){
System.out.println(getName()+"生产者准备生产集合元素!");
try {
Thread.sleep(200);
bq.put(strArr[i%4]);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(getName()+"生产者完成!"+bq);
}
}
}
class Consumer extends Thread{
private BlockingQueue<String> bq; public Consumer(String name, BlockingQueue<String> bq) {
super(name);
this.bq = bq;
}
public void run(){
while(true){
System.out.println(getName()+"消费者准备消费集合元素!");
try {
Thread.sleep(200);
bq.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(getName()+"消费者完成!"+bq);
}
}
}
上一篇:PKI 笔记


下一篇:活代码LINQ——04