今天我们巩固一下前几节所学到的线程
我们希望写一个程序,可以不断的输入,不断的输出
首先我们用以前的方式编写
接下来我们来看一段代码
/**
* 不断进行输入一个人的姓名和性别操作,同时进行输出
* @author lover
*
*/
class resourse{
int x=1;
private String name;
private String sex;
boolean flag=false;
public void chushi(String name,String sex){
this.name=name;
this.sex=sex;
}
public synchronized void input(){
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
if(x==1){
this.chushi("王海学", "男");
}else{
this.chushi("邵玥", "小仙女");
}
x++;
x=x%2;
flag=true;
notifyAll();
}
}
public synchronized void output(){
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(Thread.currentThread().getName()+"..........."+this.name+"----"+this.sex);
flag=false;
notifyAll();
}
}
}
class put implements Runnable{
resourse r;
public put(resourse r){
this.r=r;
}
public void run(){
while(true){
r.input();
}
}
}
class out implements Runnable{
resourse r;
public out(resourse r){
this.r=r;
}
public void run(){
while(true){
r.output();
}
}
}
public class Text {
public static void main(String[] args) {
resourse r=new resourse();
put p=new put(r);
out o=new out(r);
Thread pp=new Thread(p);
Thread oo=new Thread(o);
pp.start();
oo.start();
}
}
我们来看看输出
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
接下来我们将改写成jdk1.5之后的代码即使用锁
代码如下
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 不断进行输入一个人的姓名和性别操作,同时进行输出
* @author lover
*
*/
class resourse{
int x=1;
private String name;
private String sex;
boolean flag=false;
private Lock lock=new ReentrantLock();
private Condition con=lock.newCondition();
public void input(){
lock.lock();
try{
while(flag){
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(x==1){
this.chushi("王海学", "男");
}else{
this.chushi("邵玥", "小仙女");
}
x++;
x=x%2;
flag=true;
con.signal();
}finally{
lock.unlock();
}
}
public void output(){
lock.lock();
try{
while(!flag){
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"..........."+this.name+"----"+this.sex);
flag=false;
con.signal();
}finally{
lock.unlock();
}
}
public void chushi(String name,String sex){
this.name=name;
this.sex=sex;
}
}
class put implements Runnable{
resourse r;
public put(resourse r){
this.r=r;
}
public void run(){
while(true){
r.input();
}
}
}
class out implements Runnable{
resourse r;
public out(resourse r){
this.r=r;
}
public void run(){
while(true){
r.output();
}
}
}
public class Text {
public static void main(String[] args) {
resourse r=new resourse();
put p=new put(r);
out o=new out(r);
Thread pp=new Thread(p);
Thread oo=new Thread(o);
pp.start();
oo.start();
}
}
输出如下
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男
Thread-1...........邵玥----小仙女
Thread-1...........王海学----男