博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java多线程相关介绍
阅读量:3906 次
发布时间:2019-05-23

本文共 3384 字,大约阅读时间需要 11 分钟。

1.线程的创建

三种创建线程的方法:

  1. 通过实现 Runnable 接口

以下的代码文件名RunnableDemo.java

package com.company;class RunnableDemo implements Runnable {
private Thread t; private String threadName; RunnableDemo( String name) {
threadName = name; System.out.println("Creating " + threadName ); } public void run() {
System.out.println("Running " + threadName ); try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i); // 让线程睡眠一会 Thread.sleep(50); } }catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () {
System.out.println("Starting " + threadName ); if (t == null) {
t = new Thread (this, threadName); t.start (); } }}

以下的代码文件名TestThread .java

package com.company;public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1"); R1.start(); RunnableDemo R2 = new RunnableDemo( "Thread-2"); R2.start(); } }
  1. 通过继承 Thread 类本身

以下的代码文件名ThreadDemo.java

class ThreadDemo extends Thread {
private Thread t; private String threadName; ThreadDemo( String name) {
threadName = name; System.out.println("Creating " + threadName ); } public void run() {
System.out.println("Running " + threadName ); try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i); // 让线程睡眠一会 Thread.sleep(50); } }catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () {
System.out.println("Starting " + threadName ); if (t == null) {
t = new Thread (this, threadName); t.start (); } }}

以下的代码文件名TestThread.java

public class TestThread {
public static void main(String args[]) {
ThreadDemo T1 = new ThreadDemo( "Thread-1"); T1.start(); ThreadDemo T2 = new ThreadDemo( "Thread-2"); T2.start(); } }
  1. 通过 Callable 和 Future 创建线程
public class CallableThreadTest implements Callable
{
public static void main(String[] args) {
CallableThreadTest ctt = new CallableThreadTest(); FutureTask
ft = new FutureTask<>(ctt); for(int i = 0;i < 100;i++) {
System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i); if(i==20) {
new Thread(ft,"有返回值的线程").start(); } } try {
System.out.println("子线程的返回值:"+ft.get()); } catch (InterruptedException e) {
e.printStackTrace(); } catch (ExecutionException e) {
e.printStackTrace(); } } @Override public Integer call() throws Exception {
int i = 0; for(;i<100;i++) {
System.out.println(Thread.currentThread().getName()+" "+i); } return i; } }

转载地址:http://vhqen.baihongyu.com/

你可能感兴趣的文章
c、 c++、 java 基本数据类型 对比辨析
查看>>
自然语言处理(NLP)四步流程:Embed->Encode->Attend->Predict
查看>>
python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
查看>>
[转发]机器学习资源大全
查看>>
《数学之美》知识点详细总结
查看>>
机器学习 数据挖掘 数据集划分 训练集 验证集 测试集
查看>>
从不同角度看机器学习的几种学习方式
查看>>
数据挖掘 NLP 之 文本挖掘 文本处理 通用流程
查看>>
NLP 主题抽取 Topic LDA代码实践 gensim包 代码
查看>>
NLP 工具包 大调查 自然语言处理工具包合集
查看>>
scrapy爬取酒店评论数据
查看>>
各框架下(tensorflow, pytorch, theano, keras)实现几个基础结构神经网络(mlp, autoencoder, CNNs, recurrent, recursive)
查看>>
软考相关英语
查看>>
[老老实实学WCF] 第四篇 初探通信--ChannelFactory
查看>>
ASP.NET 中的 Async/Await 简介
查看>>
解决Chrome中调试JS提示“Uncaught TypeError: Cannot use 'in' operator to search for”错误信息问题
查看>>
阿里巴巴java规范 第一版
查看>>
USB通信记事
查看>>
Android 编译(1)——Android编译步骤梳理
查看>>
编译器配置(1)——ARMv7,ARMv8(AArch64) 浮点配置等相关知识
查看>>