首页 技术 正文
技术 2022年11月15日
0 收藏 425 点赞 2,863 浏览 4590 个字

Java并发编程:线程的创建

<!–*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: ‘sh’; }
pre.src-bash:before { content: ‘sh’; }
pre.src-emacs-lisp:before { content: ‘Emacs Lisp’; }
pre.src-R:before { content: ‘R’; }
pre.src-perl:before { content: ‘Perl’; }
pre.src-java:before { content: ‘Java’; }
pre.src-sql:before { content: ‘SQL’; }

table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>–>*/–>

code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}

Java并发编程:线程的创建

Table of Contents

在Java中线程的创建主要有两种,一种是通过继承抽象类Thread,一种是通过实现Runnable接口。当然,还有Concurent包里面的Callable和Future也可以算是一种。

1 Thread

我们先来看一下,使用Thread如何创建线程:

public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("Current thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.start();
}
}
}
Current thread name is: Thread-0
Current thread name is: Thread-2
Current thread name is: Thread-1
Current thread name is: Thread-3
Current thread name is: Thread-5
Current thread name is: Thread-4
Current thread name is: Thread-6
Current thread name is: Thread-7
Current thread name is: Thread-8
Current thread name is: Thread-9

可以看到,我们创建来10个线程,但是执行的顺序是不确定的。可以设置优先级,默认是5,最大是10,最小是1.但是即使设置来优先级,顺序也是不能保证。

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.setPriority(i + 1);
thread.start();
}
}

优先级逐渐上升,但执行但结果还是一样。

2 Runnable

我们再来使用Runnable创建线程:

public class RunnableDemo implements Runnable {
@Override
public void run() {
System.out.println("Current runnable thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.start();
}
}
}
Current runnable thread name is: Thread-0
Current runnable thread name is: Thread-3
Current runnable thread name is: Thread-2
Current runnable thread name is: Thread-5
Current runnable thread name is: Thread-1
Current runnable thread name is: Thread-6
Current runnable thread name is: Thread-7
Current runnable thread name is: Thread-4
Current runnable thread name is: Thread-8
Current runnable thread name is: Thread-9

可以看出,实现Runnable接口接口之后但RunnableDemo类但实例还是无法直接运行的,它必须将实例对象传入Thread类,然后,才能调用Thread对象中的start()进行启动。

3 start() 和 run()

我们接下来来看一下start() 和 run()的区别:
当我们中定义新的线程类的时候,唯一覆写的方法就是run()。那么,我们能不能直接调用run()方法呢?
答案是肯定的。
我们将start()替换为run()再试一下:

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.run();
}
}
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main

结果全是主线程在运行,也就是说根据就没有新的线程启动运行。所以,使用run()方法调用时,就和一般的函数调用一样,是由当前线程进行调用的,并不会启动新的线程,然后在新的线程中运行这个run()方法。只有使用start()方法去调用,才会启动新的线程,然后,在新的线程中运行run()方法。

Date: 2017-07-04 21:37

Author: WEN YANG

Created: 2017-07-04 Tue 22:44

Emacs 25.2.1 (Org mode 8.2.10)

Validate

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918