Java高新技术——多线程与并发库(上)
Posted 李春春_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java高新技术——多线程与并发库(上)相关的知识,希望对你有一定的参考价值。
本系列文章旨在分享Java5多线程与并法库的高级应用示例,所用到的大多数类均在java.util.concurrent包下。
传统线程技术回顾
package ustc.lichunchun.thread;
/*
* 创建线程的两种传统方式
*/
public class TraditionalThread {
public static void main(String[] args) {
//在Thread子类覆盖的run方法中编写运行代码
Thread t1 = new Thread(){
public void run(){
while(true){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("1: " + Thread.currentThread().getName());
System.out.println("2: " + this.getName());
}
}
};
t1.start();
//-----------------------------------------------------------
//在传递给Thread对象的Runnable对象的run方法中编写代码
Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("1: " + Thread.currentThread().getName());
}
}
});
t2.start();
//-----------------------------------------------------------
//涉及知识点:匿名内部类对象的构造方法如何调用父类的非默认构造方法
new Thread(new Runnable(){
@Override
public void run() {
while(true){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Runnable: " + Thread.currentThread().getName());
}
}
}){
public void run(){
while(true){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Thread: " + Thread.currentThread().getName());
}
}
}.start();//Thread: Thread-2
}
}
生产者消费者模式回顾
package ustc.lichunchun.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Res{
private String name;
private int count = 1;
private boolean flag;
private Lock lock = new ReentrantLock();
private Condition producer_con = lock.newCondition();
private Condition consumer_con = lock.newCondition();
public void set(String name){
lock.lock();
try{
while(flag){
try{
producer_con.await();
}catch(InterruptedException e){
//e.printStackTrace();
}
}
this.name = name + "-" + count;
count++;
System.out.println(Thread.currentThread().getName()+ "......生产者 ...... " + this.name);
this.flag = true;
consumer_con.signal();
}finally{
lock.unlock();
}
}
public void get(){
lock.lock();
try{
while(!flag){
try{
consumer_con.await();
}catch(InterruptedException e){
//e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+ "...消费者 ... " + this.name);
this.flag = false;
producer_con.signal();
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
private Res r;
Producer(Res r){
this.r = r;
}
@Override
public void run() {
while(true){
r.set("小龙虾");
}
}
}
class Consumer implements Runnable{
private Res r;
Consumer(Res r){
this.r = r;
}
@Override
public void run() {
while(true){
r.get();
}
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Res r = new Res();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t0 = new Thread(pro);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
Thread t3 = new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
以上是关于Java高新技术——多线程与并发库(上)的主要内容,如果未能解决你的问题,请参考以下文章