java: Thread 和 runnable线程类
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() ; // 一个线程开始卖票
}
}
java: Thread 和 runnable线程类的更多相关文章
- Java带参数的线程类ParameterizedThread——即如何给Thread传递参数
在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...
- Java Thread and runnable
java中可有两种方式实现多线程, 一种是继承Thread类,(Thread本身实现了Runnable接口,就是说需要写void run 方法,来执行相关操作) 一种是实现Runnable接口 sta ...
- Java - Thread 和 Runnable实现多线程
Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过jav ...
- java核心-多线程(4)-线程类基础知识
1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系 ...
- java Thread和Runnable区别
①Thread类实现了Runnable接口,主要构造方法为Thread(Runnable target).Thread(Runnable target,String name).Thread(Stri ...
- 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 i ...
- Java Thread线程控制
一.线程和进程 进程是处于运行中的程序,具有一定的独立能力,进程是系统进行资源分配和调度的一个独立单位. 进程特征: A.独立性:进程是系统中独立存在的实体,可以拥有自己独立的资源,每个进程都拥有自己 ...
- Java多线程并发01——线程的创建与终止,你会几种方式
本文开始将开始介绍 Java 多线程与并发相关的知识,多谢各位一直以来的关注与支持.关注我的公众号「Java面典」了解更多 Java 相关知识点. 线程的创建方式 在 Java 中,用户常用的主动创建 ...
- Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别
一. java中实现线程的方式有Thread和Runnable Thread: public class Thread1 extends Thread{ @Override public void r ...
随机推荐
- rc.local文件
rc.local用于自启动一些服务. 查看有哪些自启动服务: cat /etc/rc.local
- ajax 如何接受 PHP页面返回的json数组
JSON JSON(JavaScript Object Notation)是Douglas Crockford提出的.他是一个轻量级的数据交换格式,基于JavaScript对象字面量. 我们可以将之前 ...
- linux 中更改用户权限和用户组的命令chmod,chgrp实例
linux 中更改用户权限和用户组的命令实例; 增加权限给当前用户 chmod +wx filename chmod -R 777 /upload 用户组 chgrp -R foldname zdz ...
- grid style
<style type="text/css"> .g_grid tr{ border-left:none; border-right:none; border-top: ...
- Sql Server 中一个非常强大的日期格式化函数
Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0)-- 05 16 2006 10:57AMSelect CONV ...
- jQuery Mobile应用之火车票查询
效果图: 在CMD中输入如下代码 corsproxy (前提是有node.js环境,并先安装corsproxy) html: <!DOCTYPE html> <html> &l ...
- Android 本地/网路下载图片实现放大缩小
Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...
- Metro-Ural119递推
Time limit: 0.5 second Memory limit: 64 MB Many of SKB Kontur programmers like to get to work by Met ...
- sql中文日期格式转换(xxxx年x月x日)
) set @dd='2014年10月1日' select replace(replace( replace(@dd,'日',''),'月','-'),'年','-') 别人的方法 )='2012年1 ...
- JS验证码
1.引用jquery 2.Html页面 <div> <canvas id="canvas" style="cursor: pointer; height ...