Java SE 多线程 线程通信
package thread;
class QQ{
private String name;
private String sex;
boolean flag=false;
public synchronized void put(String name,String sex){
if(flag)
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.name=name;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sex=sex;
flag=true;
notify();
}
public synchronized void get(){
if(!flag)
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name +"----------->"+sex);
flag=false;
notify();
}
}
class Producer1 implements Runnable{
QQ qq=null;
public Producer1(QQ qq){
this.qq=qq;
}
public void run() {
int i=0;
while(true){
if(i==0){
qq.put("zxx", "nan");
}else{
qq.put("cq", "nv");
}
i=(i+1)%2;
}
}
}
class Consumer1 implements Runnable{
QQ qq=null;
public Consumer1(QQ qq){
this.qq=qq;
}
public void run() {
while(true){
qq.get();
}
}
}
public class ThreadCommunciation {
public static void main(String[] args) {
QQ qq=new QQ();
new Thread(new Producer1(qq )).start();
new Thread(new Consumer1(qq)).start();
}
}
相关文档:
比如java中常用的运算符
一 符号++ ,+,--,-
有时这个符号拼凑起来也有点复杂
比如这样一个运算式
int i=3;
i+++i-i++-++i
+ -运算符的优先级 低于++,-- 先运算++,--
可以将上面的式子拆开
i++ + i - i++ - ++i
这样是不是容易多了
先来个简单点的
1 K++
int k=0;
System.out.println(K++)
System.o ......
package thread;
class TestThread extends Thread {
public void run(){
while(true){
System.out.println(Thread.currentThread().getName());
}
}
}
public class ThreadDemo {
/**
* @param args
*/
public static void ......
值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中。==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相 ......
win7(windows7)下java环境变量配置方法
windows7下java环境变量配置方法:
1.用鼠标右击“我的电脑”->属性
选择左边导航的“高级系统设置”选项,然后这回熟悉了吧?
继续选择右下角的& ......