Java:创建线程
Java定义了两种创建线程的方法:
1.实现Runnable接口
要实现Runnable接口,只需简单地实现run()方法即可,声明如下:public void run()
在run()方法中,可以定义构成新线程的代码。需重点理解的是,run()可以调用其它方法、使用其他类和声明变量,就像主线程能做的那样。唯一的区别是:run()方法是程序的另一个并发的执行线程的进入店,这个线程在run()方法返回时结束。
创建新线程以后,直到调用了它的start()方法后才会执行。本质上,start()执行对run()的调用。
// Create a second thread.
public class NewThread implements Runnable{
Thread t;
NewThread(){
t=new Thread(this,"Demo Thread");
System.out.println("Child thread: "+t);
t.start();
} // This is the entry point of the second thread.
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("Child thread: "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e){
System.out.println("Child thread interrupted");
}
System.out.println("Exiting child thread");
}
} class ThreadDemo{
public static void main(String args[]){
new NewThread();
try{
for(int i=5;i>0;i--){
System.out.println("Main thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
System.out.println("Exiting Main thread");
}
}
输出:
Child thread: Thread[Demo Thread,5,main]
Main thread: 5
Child thread: 5
Child thread: 4
Main thread: 4
Child thread: 3
Child thread: 2
Main thread: 3
Child thread: 1
Exiting child thread
Main thread: 2
Main thread: 1
Exiting Main thread
2. 扩展Thread类本身
第二种创建线程的方法是创建一个扩展Thread类的新类,然后创建该类的一个实例。这个扩展的类必须重写run()方法,这是新线程的进入点,同时它也必须调用start()方法来执行线程。
// Create a second thread.
public class NewThread extends Thread{ NewThread(){
super("Demo thread");
System.out.println("Child thread: "+this);
start();
} // This is the entry point of the second thread.
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("Child thread: "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e){
System.out.println("Child thread interrupted");
}
System.out.println("Exiting child thread");
}
} class ThreadDemo{
public static void main(String args[]){
new NewThread();
try{
for(int i=5;i>0;i--){
System.out.println("Main thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
System.out.println("Exiting Main thread");
}
}
输出:
Child thread: Thread[Demo thread,5,main]
Main thread: 5
Child thread: 5
Child thread: 4
Main thread: 4
Child thread: 3
Child thread: 2
Main thread: 3
Child thread: 1
Exiting child thread
Main thread: 2
Main thread: 1
Exiting Main thread
同步:当两个或多个线程需要访问同一共享资源时,需要某种方式来确保资源在某一时刻只被一个线程使用,这个方式称为同步。
不使用同步的例子:
class Callme{
void call(String msg){
System.out.print("["+msg);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s){
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run(){
target.call(msg);
}
}
public class Synch {
public static void main(String args[]){
Callme target=new Callme();
Caller ob1=new Caller(target,"Hello");
Caller ob2=new Caller(target,"Synchronized");
Caller ob3=new Caller(target,"World");
try{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e){
System.out.println("Interrupted");
}
}
}
输出:
[Hello[World[Synchronized]
]
]
使用同步,只需简单地在call()的定义前面加上关键字synchronized即可
class Callme{
synchronized void call(String msg){
输出:
[Hello]
[Synchronized]
[World]
当不能在类相应的方法中添加synchronized关键字时,只需要将对该类方法的访问置于一个synchronized快中
public void run(){
synchronized(target){
target.call(msg);
}
}
Java:创建线程的更多相关文章
- -1-5 java 多线程 概念 进程 线程区别联系 java创建线程方式 线程组 线程池概念 线程安全 同步 同步代码块 Lock锁 sleep()和wait()方法的区别 为什么wait(),notify(),notifyAll()等方法都定义在Object类中
本文关键词: java 多线程 概念 进程 线程区别联系 java创建线程方式 线程组 线程池概念 线程安全 同步 同步代码块 Lock锁 sleep()和wait()方法的区别 为什么wait( ...
- 操作系统实现线程的几种模式 和 java创建线程的3个方式
操作系统实现线程的几种模式 和 java创建线程的3个方式 这是两个概念 在操作系统中,线程可以实现在用户模式下,也可以实现在内核模式下,也可以两者结合实现. 1.实现线程的三种方式: (1)继承t ...
- Java并发编程:Java创建线程的三种方式
目录 引言 创建线程的三种方式 一.继承Thread类 二.实现Runnable接口 三.使用Callable和Future创建线程 三种方式的对比 引言 在日常开发工作中,多线程开发可以说是必备技能 ...
- Java创建线程的三种主要方式
Java创建线程的主要方式 一.继承Thread类创建 通过继承Thread并且重写其run(),run方法中即线程执行任务.创建后的子类通过调用 start() 方法即可执行线程方法. 通过继承Th ...
- Java创建线程的四种方式
Java创建线程的四种方式 1.继承Thread类创建线程 定义Thread类的子类,并重写该类的run方法,run()方法的内容就是该线程执行的内容 创建Thread子类的实例,即创建了线程对象. ...
- 当阿里面试官问我:Java创建线程有几种方式?我就知道问题没那么简单
这是最新的大厂面试系列,还原真实场景,提炼出知识点分享给大家. 点赞再看,养成习惯~ 微信搜索[武哥聊编程],关注这个 Java 菜鸟. 昨天有个小伙伴去阿里面试实习生岗位,面试官问他了一个老生常谈的 ...
- java创建线程的多种方式
java创建线程的四种方式 1.继承 Thread 类 通过继承 Thread 类,并重写它的 run 方法,我们就可以创建一个线程. 首先定义一个类来继承 Thread 类,重写 run 方法. 然 ...
- Java创建线程的细节分析
转载:http://shmilyaw-hotmail-com.iteye.com/blog/1880902 前言 关于线程创建的问题,可以说是老生常谈了.在刚开始学习Thread的时候基本上都会接触到 ...
- java创建线程的三种方法
这里不会贴代码,只是将创建线程的三种方法做个笼统的介绍,再根据源码添加上自己的分析. 通过三种方法可以创建java线程: 1.继承Thread类. 2.实现Runnable接口. 3.实现Callab ...
- java创建线程
创建一个线程 Java提供了两种创建线程方法: 通过实现Runable接口: http://blog.csdn.net/duruiqi_fx/article/details/52187275 通过继承 ...
随机推荐
- idea生成可执行jar
1.创建工程 ①使用idea新建一个maven工程. ②编辑工具逻辑代码 ③完成代码的编写后添加工具调用的main方法以接收参数 至此代码编辑过程已经基本完成 ④在maven管理依赖的时候使用idea ...
- PAT天梯赛 L2-002. 链表去重 【STL】
题目链接 https://www.patest.cn/contests/gplt/L2-002 思路 用结构体 存储 一个结点的地址 值 和下一个地址 然后从首地址开始 往下走 并且每个值的绝对值 都 ...
- Unity基本API总览
- JSON.stringify出现 "Converting circular structure to JSON"
JSON.stringify() 我们很熟悉了,将一个对象转换为json形式的字符串. 但是如果你在浏览器控制台中输出 JSON.stringify(window). 如果期望输出一段文字, 可能会 ...
- jsp和servlet的区别联系
jsp和servlet的区别联系 简单的说,SUN首先发展出SERVLET,其功能比较强劲,体系设计也很先进,只是,它输出HTML语句还是采用了老的CGI方式,是一句一句输出,所以,编写和修改HTML ...
- linux命令学习笔记(26):用SecureCRT来上传和下载文件
用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...
- STL容器特征总结与迭代器失效
Vector 内部数据结构:连续存储,例如数组. 随机访问每个元素,所需要的时间为常量. 在末尾增加或删除元素所需时间与元素数目无关,在中间或开头增加或删除元素所需时间随元素数目呈线性变化. 可动态增 ...
- Sox语音转换的相关知识
SoX-linux 里操作音频的瑞士军刀 Sox是最为著名的Open Source声音文件 格式转换工具.已经被广泛移植到Dos.windows.OS2.Sun.Next.Unix.Linux等多个操 ...
- L3-007 天梯地图(30 分)
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至少存在一条可达路线. 输 ...
- 【Python】numpy 数组拼接、分割
摘自https://docs.scipy.org 1.The Basics 1.1 numpy 数组基础 NumPy’s array class is called ndarray. ndarray. ...