java实现线程的两种方法有什么区别?
来源:互联网 发布:数据库故障应急预案 编辑:程序博客网 时间:2024/05/01 14:40
如果一个类通过继承Thread来实现多线程的话,则不适合多个线程共享资源,而通过实现Runnable就可以做到这一点,下面给lz举个例子:
Java code
class MyTheard extends Thread{
private int num = 5;//不能声明为静态的全局变量
public void run(){
for(int i=0;i<100;i++){
if(num>0){
System.out.println(Thread.currentThread().getName()+"卖票"+":"+(num--));
}
}
}
}
public class TheardDemo01 {
public static void main(String[] args) {
MyTheard th1 = new MyTheard();
MyTheard th2 = new MyTheard();
MyTheard th3 = new MyTheard();
th1.start();
th2.start();
th3.start();
}
}
输出结果:
Thread-0卖票:5
Thread-0卖票:4
Thread-0卖票:3
Thread-0卖票:2
Thread-0卖票:1
Thread-2卖票:5
Thread-2卖票:4
Thread-2卖票:3
Thread-2卖票:2
Thread-2卖票:1
Thread-1卖票:5
Thread-1卖票:4
Thread-1卖票:3
Thread-1卖票:2
Thread-1卖票:1
Java code
class MyThread implements Runnable
{
private int num = 5;
public void run(){
for(int i = 0;i<100;i++){
if(num>0){
System.out.println(Thread.currentThread().getName()+"卖票:"+" "+(num--));
}
}
}
};
public class ThreadDemo02
{
public static void main(String[] args)
{
MyThread mt = new MyThread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
输出结果:
Thread-1卖票: 5
Thread-1卖票: 2
Thread-1卖票: 1
Thread-0卖票: 4
Thread-2卖票: 3
总结:
通过Rannable接口实现接口有如下的好处:
(1)适合于多个相同的程序代码的线程去处理同一资源。
(2)可以避免由于单继承带来的局限。
(3)增强了程序的健壮性,代码能够被多个线程共享,代码和数据独立。
Java code
class MyTheard extends Thread{
private int num = 5;//不能声明为静态的全局变量
public void run(){
for(int i=0;i<100;i++){
if(num>0){
System.out.println(Thread.currentThread().getName()+"卖票"+":"+(num--));
}
}
}
}
public class TheardDemo01 {
public static void main(String[] args) {
MyTheard th1 = new MyTheard();
MyTheard th2 = new MyTheard();
MyTheard th3 = new MyTheard();
th1.start();
th2.start();
th3.start();
}
}
输出结果:
Thread-0卖票:5
Thread-0卖票:4
Thread-0卖票:3
Thread-0卖票:2
Thread-0卖票:1
Thread-2卖票:5
Thread-2卖票:4
Thread-2卖票:3
Thread-2卖票:2
Thread-2卖票:1
Thread-1卖票:5
Thread-1卖票:4
Thread-1卖票:3
Thread-1卖票:2
Thread-1卖票:1
Java code
class MyThread implements Runnable
{
private int num = 5;
public void run(){
for(int i = 0;i<100;i++){
if(num>0){
System.out.println(Thread.currentThread().getName()+"卖票:"+" "+(num--));
}
}
}
};
public class ThreadDemo02
{
public static void main(String[] args)
{
MyThread mt = new MyThread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
输出结果:
Thread-1卖票: 5
Thread-1卖票: 2
Thread-1卖票: 1
Thread-0卖票: 4
Thread-2卖票: 3
总结:
通过Rannable接口实现接口有如下的好处:
(1)适合于多个相同的程序代码的线程去处理同一资源。
(2)可以避免由于单继承带来的局限。
(3)增强了程序的健壮性,代码能够被多个线程共享,代码和数据独立。
- java实现线程的两种方法有什么区别?
- 两种实现多线程的方法有什么区别
- Java线程的两种实现方法
- java两种实现线程的方法
- 实现接口成员的两种方式有何区别有什么用途
- JAVA线程之一:实现一个线程的两种方法
- 线程的sleep()方法和yield()方法有什么区别?
- 线程的sleep()方法和yield()方法有什么区别?
- java中创建线程的两种方法的区别
- java中创建线程的两种方法及其区别
- [Java]Java实现线程操作的两种方法
- JAVA的线程对象实现的两种方法
- 在Java中实现线程的两种方法
- java当中实现线程的两种方法
- JAVA中实现线程的两种方法
- java多线程㈠—实现线程的两种方法
- Java中线程实现的两种方法
- Java--线程的互斥实现(两种方法)
- Python 命名原则
- IPv6协议及实验
- Android 4.0 ICS SystemUI浅析——StatusBar结构分析
- #杂问#wireshark的reassemble tcp含义
- Human Gene Functions(最长公共子序列变形题)
- java实现线程的两种方法有什么区别?
- linux下通过JNI用C/C++中调用JAVA类
- 移植QT
- 书籍推荐
- 初学spring:web应用程序中使用Spring
- 移动web app开发必备
- C++语言复习-命名空间
- 策略模式
- Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析