为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地6264312 2018-12-06 23:45:17
以下是用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 = 0Thread运行: 线程A,i = 1Thread运行: 线程A,i = 2Thread运行: 线程A,i = 3Thread运行: 线程A,i = 4Thread运行: 线程A,i = 5Thread运行: 线程A,i = 6Thread运行: 线程A,i = 7Thread运行: 线程A,i = 8Thread运行: 线程A,i = 9Thread运行: 线程A,i = 10Thread运行: 线程A,i = 11Thread运行: 线程A,i = 12Thread运行: 线程A,i = 13Thread运行: 线程A,i = 14Thread运行: 线程A,i = 15Thread运行: 线程A,i = 16Thread运行: 线程A,i = 17Thread运行: 线程A,i = 18Thread运行: 线程A,i = 19Thread运行: 线程A,i = 20Thread运行: 线程A,i = 21Thread运行: 线程A,i = 22Thread运行: 线程A,i = 23Thread运行: 线程A,i = 24Thread运行: 线程A,i = 25Thread运行: 线程A,i = 26Thread运行: 线程A,i = 27Thread运行: 线程A,i = 28Thread运行: 线程A,i = 29Thread运行: 线程A,i = 30Thread运行: 线程A,i = 31Thread运行: 线程A,i = 32Thread运行: 线程A,i = 33Thread运行: 线程A,i = 34Thread运行: 线程A,i = 35Thread运行: 线程A,i = 36Thread运行: 线程A,i = 37Thread运行: 线程A,i = 38Thread运行: 线程A,i = 39Thread运行: 线程A,i = 40Thread运行: 线程A,i = 41Thread运行: 线程A,i = 42Thread运行: 线程A,i = 43Thread运行: 线程A,i = 44Thread运行: 线程A,i = 45Thread运行: 线程A,i = 46Thread运行: 线程A,i = 47Thread运行: 线程A,i = 48Thread运行: 线程A,i = 49Thread运行: 线程B,i = 0Thread运行: 线程B,i = 1Thread运行: 线程B,i = 2Thread运行: 线程B,i = 3Thread运行: 线程B,i = 4Thread运行: 线程B,i = 5Thread运行: 线程B,i = 6Thread运行: 线程B,i = 7Thread运行: 线程B,i = 8Thread运行: 线程B,i = 9Thread运行: 线程B,i = 10Thread运行: 线程B,i = 11Thread运行: 线程B,i = 12Thread运行: 线程B,i = 13Thread运行: 线程B,i = 14Thread运行: 线程B,i = 15Thread运行: 线程B,i = 16Thread运行: 线程B,i = 17Thread运行: 线程B,i = 18Thread运行: 线程B,i = 19Thread运行: 线程B,i = 20Thread运行: 线程B,i = 21Thread运行: 线程B,i = 22Thread运行: 线程B,i = 23Thread运行: 线程B,i = 24Thread运行: 线程B,i = 25Thread运行: 线程B,i = 26Thread运行: 线程B,i = 27Thread运行: 线程B,i = 28Thread运行: 线程B,i = 29Thread运行: 线程B,i = 30Thread运行: 线程B,i = 31Thread运行: 线程B,i = 32Thread运行: 线程B,i = 33Thread运行: 线程B,i = 34Thread运行: 线程B,i = 35Thread运行: 线程B,i = 36Thread运行: 线程B,i = 37Thread运行: 线程B,i = 38Thread运行: 线程B,i = 39Thread运行: 线程B,i = 40Thread运行: 线程B,i = 41Thread运行: 线程B,i = 42Thread运行: 线程B,i = 43Thread运行: 线程B,i = 44Thread运行: 线程B,i = 45Thread运行: 线程B,i = 46Thread运行: 线程B,i = 47Thread运行: 线程B,i = 48Thread运行: 线程B,i = 49 也就是运行完一个线程后,在运行另一个线程,并没有实现交互式访问 请问我的程序哪里有问题 啊?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 572 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信