2.27 学习记录
作者:互联网
多线程:
java中多线程需要继承Thread类或Runable(Callable类)接口来完成定义。
线程启动的主方法需要覆写Thread类中的run方法实现。
class 类名称 extends Thread{//继承Thread类
属性; //类中定义属性
方法; //类中定义方法
public void run(){ //覆写Thread类中run()方法,此方法是线程的主体
线程主体方法;
}
}
定义一个线程操作类
class MyThread extends Thread{ //这就是一个多线程的操作类
private String name; //定义类中的属性
public MyThread(String name){ //定义构造方法
this.name = name;
}
@Override
public void run(){ //覆写run()方法,作为线程的主操作方法
for(int x = 0;x < 200;x++){
System.out.println(this.name + "-->" + x);
}
}
}
public class TestDemo{ //主类
public static void main(String[] args){
MyThread mt1 = new MyThread("线程A");//实例化多线程类对象
MyThread mt2 = new MyThread("线程B");
MyThread mt3 = new MyThread("线程C");
mt1.start();//启动多线程
mt2.start();
mt3.start();
}
}
可能的程序执行结果: 线程A --> 0
线程B --> 0
线程C --> 0
线程C --> 1
(后面省略输出....)
如果直接调用run()方法,并不能启动多线程,多线程启动的唯一方法就是Thread类中的start()方法,调用这个方法执行的方法体是run()方法定义的代码。)
为什么多线程启动不是调用run()而必须调用start()?
因为多线程需要操作系统的支持,使用Thread类的start()方法不仅要启动多线程的执行代码,还要根据不同的操作系统进行资源的分配。
实现Runnable类
使用Thread类的确可以方便地进行多线程的实现,但是这种方式最大的缺点就是单继承的问题,为此,在java中也可以利用Runnable接口来实现多线程,而这个接口的定义如下:
@FunctionalInterface
public interface Runnable{
public void run();
}
class MyThread implements Runnable{
private String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run(){
for(int x = 0;x < 200;x++){
System.out.println(this,name + "-->" + x);
}
}
}
public class TestDemo{
public static void main(String[] args){
MyThread mt1 = new MyThread("线程A");//实例化多线程对象
MyThread mt2 = new MyThread("线程B");
MyThread mt3 = new MyThread("线程C");
new Thread(mt1).start();
new Thread(mt2).start();
new Thread(mt3).start();
}
}
Runnale接口并没有提供可以被继承的start()方法,此时可以利用Thread类中提供的一个有参构造方法:public Thread(Runnable target),本方法可以接收一个Runnable类中提供的一个有参构造方法:public Thread(Runnable target),本方法可以接收一个Runable接口对象。
多线程两种的两种实现方式及区别:
1.多线程的两种实现方式都需要一个线程的主类,而这个类可以实现Runable接口或继承Thread类,不管使用何种方式都必须在子类中覆写run()方法,此方法为线程的主方法。
2.Thread类是Runnable接口的子类,而且使用Runnable接口可以避免单继承局限,并且可以更加方便地实现数据共享的概念。
利用Runnale接口来实现多线程
class MyThread implements Runable{ //线程的主体类
private int ticket = 5; //一共5张票
@Override
public void run(){ //线程主方法
for(int x = 0;x < 50;x++){ //循环50次
if(this.ticket > 0){
System.out.println("卖票,ticket =" + this.ticket --);//线程操作方法
}
}
}
}
public class TestDemo{
public static void main(String[] args) throws Exception{
MyTread mt = new MyThread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
可能的程序执行结果: 卖票,ticket = 5
卖票,ticket = 4
卖票,ticket = 3
卖票,ticket = 1
卖票,ticket = 2
通过继承Thread类实现多线程
class MyThread extends Thread{
private int ticket = 5;
@Override
public void run(){
for(int x = 0;x < 50;x++){
if(this.ticket > 0){
System.out.println("卖票,ticket" + this.ticket --);
}
}
}
}
public class TestDemo{
public static void main(String[] args) throws Exception{
MyThread mt1 = new MyThread();
MyThread mt2 = new MyThread();
MyThread mt3 = new MyThread();
mt1.start();
mt2.start();
mt3.start();
可能的程序执行结果: 卖票,ticket = 5
卖票,ticket = 5
卖票,ticket = 4
卖票,ticket = 3
卖票,ticket = 5
卖票,ticket = 4
trh_csdn
发布了48 篇原创文章 · 获赞 9 · 访问量 7866
私信
关注
标签:Thread,记录,public,学习,MyThread,线程,ticket,多线程,2.27 来源: https://blog.csdn.net/trh_csdn/article/details/104532710