多线程的两种构造方法及其对比(自我详细总结)加强理解

首先,我们知道线程其实就是每一个进程里面有多个执行单元同时执行。(就像电脑上每一个软件被打开时,就相当于正在运行一个进程,而这个进程里面的每一项功能使用就相当于一个线程)

此外,多线程虽然是可以并发执行,但是其实是单条执行,是由于CPU运行特别快,给人一种同时执行的错觉。

下面,我将通过我的学习为大家讲解两种创建线程的方法。

(1)继承Thread类创建多线程

通过学习,对于多线程来说,我们知道多线程算是一个个的单线程来组成的。

下面是单线程的代码,通过两个代码来对比多线程与单线程的区别

public class danxiancheng{
	public static void main(String args[]){
	Mythread my=new Mythread();//创建Mythread实例对象
	my.run();//调用Mythread类的run()方法
	while(true){
		System.out.println("Main方法在运行");
		}
	}
}
class Mythread{
	public void run(){
	while(true){
		System.out.println("Mythread类的run()方法在运行");
		}
	}
}

这个单线程并没有继承Thread类,所以只能打印Mythread类里面的run()方法。

接下来是进行多线程的操作代码:

public class duoxiancheng{
	public static void main(String args[]){
		Mythread my=new Mythread();//创建Mythread的线程对象
		my.start();//开启线程
		while(true){
		System.out.println("main()方法在运行");
		}
	}
}
class Mythread extends Thread{
	public void run(){
		while(true){
			System.out.println("Mythread类的run()方法在运行");
		}
	}
}

此时,Mythread类中的run()方法和主方法中的语句也会被打印。

最后,会总结一下这两种方法的不同。

(2)通过自己建的类去实现Runnable接口创建多线程

这种方法的好处是,可以直接去实现这个接口,而继承由于语言原因,JAVA只能实现单继承,所以一旦某个类继承了别的类,就不能继承Thread类,所以就创建不了多线程了。

该方法被博主推荐使用,直接代码走起,仔细看备注:

public class duoxianchengjiekou{
	public static void main(String args[]){
		Mythread my=new Mythread();//创建Mytread实例对象
		Thread thread=new Thread(my);//创建线程对象,将实例对象作为参数传进来
		thread.start();//开启线程,使用start()方法,执行线程中run()方法
		while(true){
			System.out.println("main()方法在运行");
		}
	}
}
class Mythread implements Runnable{
	public void run(){//当开始执行start()方法时,线程开始执行
		while(true){
			System.out.println("Mythread类的run()方法在运行");
		}
	}
}

(3)两种方法的对比(通过学习中的实例来进行解释)

两种方法都可以实现多线程的创建,但通过此实例就知道其中的不同直接上代码:

实例:通过四个窗口,去出售100张火车票。(可以通过这个实例,去进行延伸,就像是【斗地主发牌】【发作业】)

通过继承Thread类来建立

public class Example04 {
    public static void main(String[] args) {
        new TicketWindow().start();//创建第一个线程对象并开启
        new TicketWindow().start();
        new TicketWindow().start();
        new TicketWindow().start();
    }
}
class TicketWindow extends Thread{
    private int tickets=100;
    @Override
    public void run() {
        while(true){
            if(tickets>0){
                Thread th=Thread.currentThread();//获取当前线程
                String th_name=th.getName();//获取当前线程名字
                System.out.println(th_name+"正在发售第"+tickets--+"张票");
            }
        }
    }
}

在这里插入图片描述
由此可以看出,每张票都被打印了4次,这是因为四个线程并没有共享100张票,而是各自出售100张,相当于创建了4个程序。

但放到现实生活中,应该是四个线程共享100张票,只建立一个程序,四个线程,单程序多线程。

如果是用实现Runnable接口的方法,则就解决了这个问题:

public class Example05 {
    public static void main(String[] args) {
        TicketWindow01 th1=new TicketWindow01();
        new Thread(th1,"窗口1").start();//创建线程并起名窗口1,开启线程
        new Thread(th1,"窗口2").start();
        new Thread(th1,"窗口3").start();
        new Thread(th1,"窗口4").start();
    }
}
class TicketWindow01 implements Runnable{//创建线程的任务类
    private int ticket=100;
    public void run(){
        while (true) {
            if (ticket > 0) {
                Thread thread1 = Thread.currentThread();
                String thread1_name = thread1.getName();
                System.out.println(thread1_name + "正在发售第" + ticket-- + "张票");
            }
        }
    }
}

在这里插入图片描述

这次就明显的看出,解决了无法共享的问题,这就是继承类在此处的弊端。

所以,重点来了

(1)通过实现接口的方式,可以使相同的代码去处理同一个资源的情况。

(2)避免了Java的单继承的局限性。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值