java: Thread 和 runnable线程类

Java有2种实现线程的方法:Thread类,Runnable接口。(其实Thread本身就是Runnable的子类)

Thread类,默认有run(), start()方法,继承Thread类,需要实现run方法

Thread多线程,不能共享资源,保证数据的的统一(以商城商品数量,售票系统票的数量为例)

例如:

public class MyThread extends Thread {
	private String name; // 定义name属性

	public MyThread(String name) {
		this.name = name;
	}

	public void run() {// 覆写run()方法
		for (int i = 0; i < 50; i++) {// 表示循环10次
			System.out.println("Thread运行:" + name + ",i = " + i);
		}
	}
}

实现1:

 此种方法说明:mt1线程执行完后,才能执行mt2线程

public class ThreadDemo01 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A") ;
		MyThread mt2 = new MyThread("线程B") ;
		mt1.run() ;	// 调用线程体
		mt2.run() ;	// 调用线程体
	}
}

  

实现2:

start()方法表示,调用系统底层方法,去抢占cpu资源,谁先抢到,谁就能优先得到cpu的调度

public class ThreadDemo02 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A");
		MyThread mt2 = new MyThread("线程B");
		mt1.start(); // 调用线程体
		mt2.start(); // 调用线程体

	}

}

  

Runnable:

public class MyThread implements Runnable { // 实现Runnable接口
	private String name; // 定义name属性

	public MyThread(String name) {
		this.name = name;
	}

	public void run() {// 覆写run()方法
		for (int i = 0; i < 50; i++) {// 表示循环10次
			System.out.println("Thread运行:" + name + ",i = " + i);
		}
	}
}

  

实现子类:

public class RunnableDemo01 {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程A");
		MyThread mt2 = new MyThread("线程B");
		new Thread(mt1).start(); // 调用线程体
		new Thread(mt2).start(); // 调用线程体
	}
}

  

推荐实现Runnable接口来使用多线程。

使用Runnable能实现资源共享,以商城/售票系统为例,售卖票

Thread例子:

public class MyThread extends Thread {// 继承Thread类
	private int ticket = 5; // 一共才5张票

	public void run() {// 覆写run()方法
		for (int i = 0; i < 50; i++) {// 表示循环10次
			if (this.ticket > 0) {
				System.out.println("卖票:ticket = " + this.ticket--);
			}
		}
	}
}

  

实现子类:

此种方法:其实票一共只有5张,但是三个线程,每个线程都能买到5张票,这样是不符合逻辑的

public class ThreadTicket {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread(); // 一个线程
		MyThread mt2 = new MyThread(); // 一个线程
		MyThread mt3 = new MyThread(); // 一个线程
		mt1.start() ;	// 开始卖票
		mt2.start() ;	// 开始卖票
		mt3.start() ;	// 开始卖票
	}

}

  

实现方法二:

public class MyThread implements Runnable {// 实现Runnable接口
	private int ticket = 5; // 一共才5张票

	public void run() {// 覆写run()方法
		for (int i = 0; i < 50; i++) {// 表示循环10次
			if (this.ticket > 0) {
				System.out.println("卖票:ticket = " + this.ticket--);
			}
		}
	}
}

  

实现子类:

此方法,能实现线程内的资源共享,不会卖出多余的票

public static void main(String[] args) {
		MyThread mt = new MyThread(); // 一个对象
		new Thread(mt).start() ;// 一个线程开始卖票
		new Thread(mt).start() ;//一个线程                new Thread(mt).start() ; // 一个线程开始卖票 } 
}

  

 

时间: 2024-08-07 06:57:56

java: Thread 和 runnable线程类的相关文章

Java带参数的线程类ParameterizedThread——即如何给Thread传递参数

在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎留言说明.本文最后给出了一种实现带运行参数的线程实现类. 在C#的基础类库中早就提供了相关的解决方案,如下是C#提供的带参数的子线程创建方法: Thread th = new Thread((param) => { Console.WriteLine(param); }); th.Start(i);

Java - Thread 和 Runnable实现多线程

Java多线程系列--"基础篇"02之 常用的实现多线程的两种方式 概要 本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程池的内容,我们以后会详细介绍:现在,先对的Thread和Runnable进行了解.本章内容包括:Thread和Runnable的简介Thread和Runnable的异同点Thread和Runnable的

Java Thread and runnable

java中可有两种方式实现多线程, 一种是继承Thread类,(Thread本身实现了Runnable接口,就是说需要写void run 方法,来执行相关操作) 一种是实现Runnable接口 start, 和主线程一起执行,执行的顺序不确定 join,线程们 先执行,当所有的子线程执行完毕后,主线程才执行操作(文章最下面的例子) // 1 Provide a Runnable object, the Thread class itself implements Runnable//~~~ 1.

java核心-多线程(4)-线程类基础知识

1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系统层面提供了.但是进程的数量和开销都有限制,并且多个进程之间的数据共享比较麻烦.另一种比较轻量的并发实现是使用线程,一个进程可以包含多个线程.线程在进程中没有数量限制, 数据共享相对简单.线程的支持跟语言是有关系的.Java 语言中支持多线程. <3>Java 中的多线程是抢占式的.这意味着一个任

java Thread和Runnable区别

package com.tonyluis; class MyThread extends Thread{ private int ticketNum; private String name; public MyThread(String name,int ticketNum){ super("Thread:"+name);//线程名 this.name =name; this.ticketNum=ticketNum; } public void run(){ int tmp=this

Thread与Runnable线程继承与实现

1 package ThreadL; 2 3 class ThreadR implements Runnable{ 4 static int i=0; 5 public void run(){ 6 for(int k=0;k<10;i++,k++){ 7 System.out.println("Liang\t" + i); 8 } 9 } 10 } 11 public class Thread2 { 12 public static void main(String[] args

NSThread - (void)start vs java Thread implements Runnable

This method spawns the new thread and invokes the receiver’s main method on the new thread. If you initialized the receiver with a target and selector, the default main method invokes that selector automatically. If this thread is the first thread de

java基础知识回顾之java Thread类学习(三)--java线程实现常见的两种方式实现好处:

总结:实现Runnable接口比继承Thread类更有优势: 1.因为java只能单继承,实现Runnable接口可以避免单继承的局限性 2.继承Thread类,多个线程不能处理或者共享同一个资源,但是实现Runnable接口可以处理同一个资源. 下面我们做个测试:验证下.车站的售票系统售票的例子,车站的各个售票口相当于各个线程,我们先使用第一种方法几继承Thread类的方式实现: 代码如下: package com.lp.ecjtu.Thread; /** * * @author Admini

java线程-Thread和Runnable的区别

进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换开销小. 线程和进程一样分为五个阶段:创建.就绪.运行.阻塞.终止. 多进程是指操作系统能同时运行多个任务(程序). 多线程是指在同一程序中有多个顺序流在执行 在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. Thread类是在java.lang包