生活随笔
收集整理的這篇文章主要介紹了
线程初步(四)--小练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天我們鞏固一下前幾節所學到的線程
我們希望寫一個程序,可以不斷的輸入,不斷的輸出
首先我們用以前的方式編寫
接下來我們來看一段代碼
/*** 不斷進行輸入一個人的姓名和性別操作,同時進行輸出* @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) {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) {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;
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) {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) {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...........王海學
----男
總結
以上是生活随笔為你收集整理的线程初步(四)--小练习的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。