スレッドThreadクラスRunnableインタフェースの違い
1:thread方式
2:runnable
package com.thread;
public class MyThread extends Thread{
private int ticket = 10;
public void run(){
for(int i =0;i<500;i++){
if(this.ticket>0){
System.out.println(Thread.currentThread().getName()+" ---->"+(this.ticket--));
}
}
}
public static void main(String[] args) {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt, " ");
Thread t2 = new Thread(mt, " ");
Thread t3 = new Thread(mt, " ");
t1.start();
t2.start();
t3.start();
}
}
2:runnable
package com.thread;
public class myRunnable implements Runnable {
private int ticket = 10;
public void run() {
for (int i = 0; i < 500; i++) {
if (this.ticket > 0) {
System.out.println(Thread.currentThread().getName() + " エ---->"
+ (this.ticket--));
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
myRunnable mt = new myRunnable();
Thread t1 = new Thread(mt, " ");
Thread t2 = new Thread(mt, " ");
Thread t3 = new Thread(mt, " ");
t1.start();
t2.start();
t3.start();
}
}