我最近在学习java线程。我想实现如下功能:有两个线程t1 t2 有一个共有变量 int i=0;线程t1负责将i的值从1自增到4然后将工作交给t2 t2将i的值从4自增到10在初始阶段两个线程同时开始t1.start();t2.start(); t2由于没有得到从t1传来的值所以处于等待状态wait();直道t1完成任务t2得到了i的值才被唤醒notify();我的问题是当t2被唤醒时需要检查条件即i的值,如果i的值不是4则t2要重新回到等待状态并且标明i的值不合法。请问如何实现此功能。由于我只是个初学者,那位大侠如果有时间可以帮忙解答一下。有具体的例子更好。谢谢大家了。
解决方案 »
- 怎样清理JAVA屏幕打印信息?
- 有有一个数学问题,请大家指教。高分哦
- 我是初学者,我有几个问题,怎么也想不明白,请大家指教,谢谢!!
- 为什么我用applet显示的菜单条在有的机上不能用applet显示,只能成为html显示。
- 一台电脑(包括机箱和显示器)有多大功率?(瓦)
- 菜鸟问题:如何将.war解压!
- JCreator 与Wireless 如何配置。在线等待中!
- 大哥们,小弟我准备学JAVA了给推荐几本好书吧!小弟不胜感激啊!
- 众所周知,JPanel容器是JComponet的子类,那么JPanel和JComponent有什么区别
- 关于TCP/IP下字节流传输的问题
- 整形与长整形问题
- 帮看看 socket有什么问题
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
MyTest t = new MyTest();
t.new T2().start();
t.new T1().start();
} private int i = 0; private Object lock = new Object(); class T1 extends Thread {
@Override
public void run() {
synchronized (lock) {
for (int k = 0; k <= 4; k++) {
i = k;
}
System.out.println("T1 Finished....");
lock.notify();
}
}
} class T2 extends Thread {
@Override
public void run() {
synchronized (lock) {
while (i < 4) {
try {
System.out.println("Warting....");
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Continue....");
for (int k = 5; k <= 10; k++) {
i = k;
}
System.out.println("T2 Finished....");
}
}
}
}
仅供参考 http://www.java2000.net/viewthread.jsp?tid=5750
private int j;
Resoure(){
j=0;
}
public void increase()
{
j++;
}
public void printf(){
System.out.println(j);
}
public int getRes(){
return j;
}
}
class InThread implements Runnable {
private Resoure r;
InThread (Resoure r){
this.r=r;
}
public void run() {
while(true){
if(r.getRes()<4){
synchronized (r){
r.increase();
System.out.print("in firstThread ");
r.printf();
r.notify();
}
}else {synchronized(r){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}class DeThread implements Runnable{
Resoure r;
DeThread (Resoure s){
this.r=s;
}
public void run() {
while(true){
if(r.getRes()==10)break;
if(r.getRes()>=4){
synchronized (r){
r.increase();
System.out.print("in secondThread ");
r.printf();
r.notify();
}
}else {synchronized(r){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class TestThread{
public static void main(String args[]){
Resoure r=new Resoure();
InThread t1=new InThread(r);
DeThread t2 =new DeThread(r);
new Thread(t1).start();
new Thread(t2).start();
}
}
参考下吧
//用来区别t1,t2的标记
private int flag;
private static int i=0;
//对象锁
private String s="";
public void run(){synchronized(s){
if(flag==2){
//先让T2等待
try {
s.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true){
i++;
System.out.println("t2线乘运行i="+i);
if(i==10){
//通知t1性来
s.notify();
break;
}
}
}else{
while(true){
i++;
System.out.println("t1线乘运行i="+i);
if(i==4){
//i等于4了通知t2来执行
s.notify();try {
s.wait();
} catch (InterruptedException e) { e.printStackTrace();
}
break;
}}
}
}}
public static void main(String[] args){
T t2=new T();
t2.flag=2;
t2.start();
try{
t2.sleep(1000);
}catch(Exception e){}
T t1=new T();
t1.flag=1;
t1.start();}
}
不用wait()就简单点`