java多线程和synchronized应用实例
2013-02-15 16:19
621 查看
class Info{
private String str1="祁煜";
private String str2="学生 ";
private boolean flag = false;
public synchronized voidset(String str1,String str2){
if(!flag){
try{
super.wait();
}catch(Exceptione){
e.printStackTrace();
}
}
this.setStr1(str1);
try{
Thread.sleep(300);
}catch(Exception e){
e.printStackTrace();
}
this.setStr2(str2);
flag=false;
super.notify();
}
public synchronized void get(){
if(flag){
try{
super.wait();
}catch(Exceptione){
e.printStackTrace();
}
}
try{
Thread.sleep(300);
}catch(Exceptione){
e.printStackTrace();
}
System.out.println(this.getStr1()+"--->"+this.getStr2());
flag=true;
super.notify();
}
public void setStr1(String str1){
this.str1=str1;
}
public void setStr2(String str2){
this.str2=str2;
}
public String getStr1(){
return this.str1;
}
public String getStr2(){
return this.str2;
}
}
class Producer implements Runnable{
private Info info = null;
public Producer(Info info){
this.info=info;
}
public void run(){
boolean flag= false;
for(int i=0;i<50;i++){
if(flag){
this.info.set("祁煜","学生");
flag=false;
}else{
this.info.set("森森","池院");
flag=true;
}
}
}
}
class consume implements Runnable{
private Info info = null;
public consume(Info info){
this.info=info;
}
public void run(){
for(int i=0;i<50;i++){
this.info.get();
}
}
}
public class aynthronized0{
public static void main(String args[]){
Info info =new Info();
Producerpro= new Producer(info);
consumecon=new consume(info);
newThread(pro).start();
newThread(con).start();
}
}
private String str1="祁煜";
private String str2="学生 ";
private boolean flag = false;
public synchronized voidset(String str1,String str2){
if(!flag){
try{
super.wait();
}catch(Exceptione){
e.printStackTrace();
}
}
this.setStr1(str1);
try{
Thread.sleep(300);
}catch(Exception e){
e.printStackTrace();
}
this.setStr2(str2);
flag=false;
super.notify();
}
public synchronized void get(){
if(flag){
try{
super.wait();
}catch(Exceptione){
e.printStackTrace();
}
}
try{
Thread.sleep(300);
}catch(Exceptione){
e.printStackTrace();
}
System.out.println(this.getStr1()+"--->"+this.getStr2());
flag=true;
super.notify();
}
public void setStr1(String str1){
this.str1=str1;
}
public void setStr2(String str2){
this.str2=str2;
}
public String getStr1(){
return this.str1;
}
public String getStr2(){
return this.str2;
}
}
class Producer implements Runnable{
private Info info = null;
public Producer(Info info){
this.info=info;
}
public void run(){
boolean flag= false;
for(int i=0;i<50;i++){
if(flag){
this.info.set("祁煜","学生");
flag=false;
}else{
this.info.set("森森","池院");
flag=true;
}
}
}
}
class consume implements Runnable{
private Info info = null;
public consume(Info info){
this.info=info;
}
public void run(){
for(int i=0;i<50;i++){
this.info.get();
}
}
}
public class aynthronized0{
public static void main(String args[]){
Info info =new Info();
Producerpro= new Producer(info);
consumecon=new consume(info);
newThread(pro).start();
newThread(con).start();
}
}
相关文章推荐
- Java多线程--synchronized ,Condition,BlockingQueue应用实例
- 【多线程】应用Java多线程实例
- java多线程学习笔记(三) ——多线程实例火车票问题+同步synchronized初接触
- Java多线程应用实例
- java多线程售票程序实例及synchronized理解
- java中synchronize关键字的理解 在多线程环境下的应用: synchronized 关键字,它包括两种用法:synchronized 方法和 synchronized 块。 1. syn
- Java多线程与并发应用-(2)-线程互斥synchronized
- Java【多线程知识总结(7)】多线程同步问题-关于synchronized代码块和synchronized方法的应用
- java中多线程应用实例
- java多线程-多线程同步代码块-银行存款实例-锁synchronized
- Java多线程的应用实例
- Java多线程程序中synchronized修饰方法的使用实例
- Java多线程的synchronized应用
- java高级进阶关于java多线程的应用 ThreadLocal多线程实例详解
- java高级进阶关于java多线程的应用 ThreadLocal多线程实例详解
- java多线程synchronized的应用
- 跟着实例学习java多线程3-synchronized的多种写法有何区别?
- Java多线程/并发05、synchronized应用实例:线程间操作共享数据
- Java多线程之Synchronized应用在static和非static方法上的区别
- Java基础(高级)——多线程的理解和Synchronized实例,以及线程间通信,wait,notify等方法