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

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

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

慕的地6264312
浏览 573回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java