以下是用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
也就是运行完一个线程后,在运行另一个线程,并没有实现交互式访问
请问我的程序哪里有问题 啊?
讲到多线程一般是为了实现资源共享,而线程如果要实现资源共享,继承Thread类是不能实现资源共享的,实现Runable接口就可以资源共享。下面我给出一个火车票卖票的例子为例说明多线程。
假如只有5张火车票,多三个售票点(三个线程)在卖票,这三个售票点都需要根据余票(资源)进行售票,只有再余票ticket>0的时候才可以卖票。那么就有一下的代码实例
SyncDemo1
public class SyncDemo1 { public static void main(String args[]) { MyRunableThread1 mrt=new MyRunableThread1(); Thread t1=new Thread(mrt,"t1"); Thread t2=new Thread(mrt,"t2"); Thread t3=new Thread(mrt,"t3"); t1.start(); t2.start(); t3.start(); } }
MyRunableThread1
public class MyRunableThread1 implements Runnable { private int ticket = 5; @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 100; i++) if (ticket > 0) { try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"在卖票:剩余ticket=" + ticket--); } } }
但是上面的多线程并没有实现同步,更多关于同步都可以看我整理的一篇博客
http://www.cnblogs.com/xwdreamer/archive/2011/11/20/2296931.html