日期:2014-05-20  浏览次数:20815 次

Java多线程实现线程交互式访问问题


以下是用Eclipse编写的2个Java文件中的类:

1、threaddemo.java文件:

package demo.java.threaddemo;

public class ThreadDemo01 {
 public static void main(String[] args) {
  MyThread t1 = new MyThread("线程A");
  MyThread t2 = new MyThread("线程B");
  t1.start();
  t2.start();  
 }

}

2、MyThread.java文件

package demo.java.threaddemo;

public class MyThread extends Thread{
 private String name;
 
 public MyThread(String name){
  this.name = name;  
 }
 
 public void run(){//覆写run方法
  for(int i = 0;i < 50;i++){
  System.out.println("Thread运行: " + name + ",i = " + i);
  }
  
 }

}

 

但是运行结果总是:

Thread运行: 线程A,i = 0
Thread运行: 线程A,i = 1
Thread运行: 线程A,i = 2
Thread运行: 线程A,i = 3
Thread运行: 线程A,i = 4
Thread运行: 线程A,i = 5
Thread运行: 线程A,i = 6
Thread运行: 线程A,i = 7
Thread运行: 线程A,i = 8
Thread运行: 线程A,i = 9
Thread运行: 线程A,i = 10
Thread运行: 线程A,i = 11
Thread运行: 线程A,i = 12
Thread运行: 线程A,i = 13
Thread运行: 线程A,i = 14
Thread运行: 线程A,i = 15
Thread运行: 线程A,i = 16
Thread运行: 线程A,i = 17
Thread运行: 线程A,i = 18
Thread运行: 线程A,i = 19
Thread运行: 线程A,i = 20
Thread运行: 线程A,i = 21
Thread运行: 线程A,i = 22
Thread运行: 线程A,i = 23
Thread运行: 线程A,i = 24
Thread运行: 线程A,i = 25
Thread运行: 线程A,i = 26
Thread运行: 线程A,i = 27
Thread运行: 线程A,i = 28
Thread运行: 线程A,i = 29
Thread运行: 线程A,i = 30
Thread运行: 线程A,i = 31
Thread运行: 线程A,i = 32
Thread运行: 线程A,i = 33
Thread运行: 线程A,i = 34
Thread运行: 线程A,i = 35
Thread运行: 线程A,i = 36
Thread运行: 线程A,i = 37
Thread运行: 线程A,i = 38
Thread运行: 线程A,i = 39
Thread运行: 线程A,i = 40
Thread运行: 线程A,i = 41
Thread运行: 线程A,i = 42
Thread运行: 线程A,i = 43
Thread运行: 线程A,i = 44
Thread运行: 线程A,i = 45
Thread运行: 线程A,i = 46
Thread运行: 线程A,i = 47
Thread运行: 线程A,i = 48
Thread运行: 线程A,i = 49
Thread运行: 线程B,i = 0
Thread运行: 线程B,i = 1
Thread运行: 线程B,i = 2
Thread运行: 线程B,i = 3
Thread运行: 线程B,i = 4
Thread运行: 线程B,i = 5
Thread运行: 线程B,i = 6
Thread运行: 线程B,i = 7
Thread运行: 线程B,i = 8
Thread运行: 线程B,i = 9
Thread运行: 线程B,i = 10
Thread运行: 线程B,i = 11
Thread运行: 线程B,i = 12
Thread运行: 线程B,i = 13
Thread运行: 线程B,i = 14
Thread运行: 线程B,i = 15
Thread运行: 线程B,i = 16
Thread运行: 线程B,i = 17
Thread运行: 线程B,i = 18
Thread运行: 线程B,i = 19
Thread运行: 线程B,i = 20
Thread运行: 线程B,i = 21
Thread运行: 线程B,i = 22
Thread运行: 线程B,i = 23
Thread运行: 线程B,i = 24
Thread运行: 线程B,i = 25
Thread运行: 线程B,i = 26
Thread运行: 线程B,i = 27
Thread运行: 线程B,i = 28
Thread运行: 线程B,i = 29
Thread运行: 线程B,i = 30
Thread运行: 线程B,i = 31
Thread运行: 线程B,i = 32
Thread运行: 线程B,i = 33
Thread运行: 线程B,i = 34
Thread运行: 线程B,i = 35
Thread运行: 线程B,i = 36
Thread运行: 线程B,i = 37
Thread运行: 线程B,i = 38
Thread运行: 线程B,i = 39
Thread运行: 线程B,i = 40
Thread运行: 线程B,i = 41
Thread运行: 线程B,i = 42
Thread运行: 线程B,i = 43
Thread运行: 线程B,i = 44
Thread运行: 线程B,i = 45
Thread运行: 线程B,i = 46
Thread运行: 线程B,i = 47
Thread运行: 线程B,i = 48
Thread运行: 线程B,i = 49

也就是运行完一个线程后,在运行另一个线程,并没有实现交互式访问

请问我的程序哪里有问题 啊?


------解决方案--------------------
线程运行是随机的,彼此之间没有任何约束的线程,能否交互运行由CPU决定
要强制交互运行,就必须让线程之间有约束,也就是线程的同步协调

修改你的线程类
Java code
public class MyThread extends Thread{
    static Object locker = new Object();
    static String[] names = {"线程A", "线程B"};
    static int index = 0;
    private String name;
  
    public MyThread(String name){
        this.name = name;   
    }
  
    public void run(){//覆写run方法
        for(int i = 0;i < 50;i++){
          synchronized(locker) {
              while (!names[index].equals(this.name)) {locker.wait();}
              System.out.println("Thread运行: " + name + ",i = " + i);
              index = (index+1)%names.length;
              locker.notifyAll();
          }
       }
    }
}