晚上好!
package Test1;class ThreadC extends Thread 
{
    public ThreadC() {
// TODO Auto-generated constructor stub
} public void run()
    {
for(int i =0;i<11;i++)
      {if(i%2==0)
 synchronized(this)
     {
 System.out.println("oushu:"+i);
 this.notifyAll();

}
     }
      }
    }public class MyClass2 {
public static void main(String[] args) {

ThreadC th1 = new ThreadC();
th1.start();
for(int i =0;i<11;i++)
{
     if(i%2==1)
     { synchronized(th1)
     {
 try {

 th1.wait(10);//解决方法,不建议是使用带参数的WAIT,希望能通过NOTIFY唤醒
 System.out.println("jishu:"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
     //  System.out.println("jishu:"+i);
     } 
            
 }

}
}
}
得出结果如下!
oushu:0
oushu:2
oushu:4
oushu:6
oushu:8
oushu:10
jishu:1
jishu:3
jishu:5
jishu:7
jishu:9现想得出如下结果,不知道如何修改!
oushu:0
jishu:1
oushu:2
jishu:3
oushu:4
jishu:5
oushu:6
jishu:7
oushu:8
jishu:9
oushu:10
   请指点,

解决方案 »

  1.   

    希望解决方法使用两个线程相互唤醒·· ·  多谢了,小弟刚学JAVA, 还请多多帮助··
      

  2.   

    我也刚学 不知道这样写 行不行class ThreadC extends Thread  {
    public ThreadC() {
    // TODO Auto-generated constructor stub
    } public void run(){
    for(int i =0;i<11;i++){
    if(i%2==0)
    synchronized(this){
    try{

    System.out.println("oushu:"+i);
    notify();
    wait();
    // this.notifyAll();
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    }
    }
    }public class MyClass2 {
    public static void main(String[] args) {
    // MyClass2 my = new
    ThreadC th1 = new ThreadC();
    th1.start();
    for(int i =0;i<11;i++){
    if(i%2==1){ 
    synchronized(th1){
    try {
    // th1.wait(10);//解决方法,不建议是使用带参数的WAIT,希望能通过NOTIFY唤醒

    th1.notify();
    th1.wait();
    System.out.println("jishu:"+i);

    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    // System.out.println("jishu:"+i);
    }  
      
    } }
    }
    }
      

  3.   

    看一下这个,生产者消费者问题:
    public class Test{
        public static void main(String []args){
         Buffer  product=new Buffer();
            Producer p=new Producer( product);
            Consumer c=new Consumer(product);
            p.start();//启动生产者线程
            c.start();//启动消费者线程
       }
    }
    //生产者
    class Producer extends Thread{
          private Buffer  product;
          public Producer(Buffer  product){
              this. product= product;
            }
         public void run(){
             for(int count=1;count<=10;count++){
             try{
               Thread.sleep(2000);
             }catch(InterruptedException e){}
             product.setproductint(count);
             System.out.println("生产:"+count);
           }
       }
    }//消费者
    class Consumer extends Thread{
         private Buffer product;
         public Consumer(Buffer product){
              this. product=product;
         }
         public void run(){
         int value;
         do{
           try{
             Thread.sleep(2000);
            }catch(InterruptedException e){}
         value= product.getproductint();
         System.out.println("消费:"+value);
         }while(value!=10);
       }
    }
    //共享缓冲区
    class Buffer{
        private int productint=0;
        private boolean enable=true;//标志是否可以进行操作
        public synchronized void setproductint(int value){
           while(!enable){
             try{
               wait();//当前线程等待,直到另一线程调用notify()方法
             }catch(InterruptedException e){}
            }
           productint=value;
           enable=false;
           notify();//唤醒正在等待的进程
          }
        public synchronized int getproductint(){
           while(enable){
           try{
             wait();
             }catch(InterruptedException e){}
            }
           enable=true;
           notify();
           return productint;
          }
    }
      

  4.   

    给你来个三个依次执行的public class Test {
    public static void main(String[] args) {
    Myt myt1 = new Myt("A",0);
    Myt myt2 = new Myt("B",1);
    Myt myt3 = new Myt("C",2);
    myt1.start();
    myt2.start();
    myt3.start();
    }
    }class Myt extends Thread {
    private static Object obj = new Object(); private static int cnt=0;

    private int code; public Myt(String threadName,int code) {
    super( threadName);
    this.code=code;
    } @Override
    public void run() {
    while (true) {
    try {
    synchronized (obj) {
    Thread.sleep(500);
    if(cnt%3==this.code){
    System.out.println(this.currentThread().getName()+":"+cnt);
    cnt++;
    obj.notifyAll();
    } else{
    obj.wait();
    }

    if(cnt>20){
    break;
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
    如果你只是两个线程,那if(cnt%3==this.code)这里3就改为2,也就是说你main方法里启动了几个,这里改成几了,当然了,线程了编号也要按顺序了,不知道能否满足你的要求
      

  5.   

    这个格式好看点public class Test {
    public static void main(String[] args) {
    Myt myt1 = new Myt("A",0);
    Myt myt2 = new Myt("B",1);
    Myt myt3 = new Myt("C",2);
    Myt myt4 = new Myt("D",3);
    Myt myt5 = new Myt("E",4);
    myt1.start();
    myt2.start();
    myt3.start();
    myt4.start();
    myt5.start();
    }
    }class Myt extends Thread {
    private static Object obj = new Object(); private static int cnt=0;

    private int code; public Myt(String threadName,int code) {
    super( threadName);
    this.code=code;
    } @Override
    public void run() {
    while (true) {
    try {
    synchronized (obj) {
    Thread.sleep(500);
    if(cnt%5==this.code){
    System.out.println(this.currentThread().getName()+":"+cnt);
    cnt++;
    obj.notifyAll();
    } else{
    obj.wait();
    }

    if(cnt>20){
    break;
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }