Java多线程(一)

线程问题,或称并发

创建线程一般有一下方法

  • 继承Thread类,重写run方法
    1 public class TestThread extends Thread{ 
    2     public void run() {
    3             System.out.println("Hello World");
    4         }  
    5     public static void main(String[] args) {
    6         Thread mThread = new TestThread();
    7         mThread.start(); 
    8     } 
    9 }

     

  • 实现Runnable接口,并实现run方法
     1 public class TestRunnable implements Runnable {
     2     public void run() { 
     3             System.out.println("Hello World");
     4         } 
     5 }
     6 
     7 public class TestRunnable {
     8     public static void main(String[] args) {
     9         TestRunnable mTestRunnable = new TestRunnable();      
    10         Thread mThread = new Thread(mTestRunnable);
    11         mThread.start(); 
    12     } 
    13 }

    注意:Runnable 不是线程而是进程,或者称为thread的一个target

    • 实现Callable接口,重写call()方法
      public class TestCallable {  public static class MyTestCallable  implements Callable {  public String call() throws Exception {  retun "Hello World";}  }  
      public static void main(String[] args) {  MyTestCallable mMyTestCallable= new MyTestCallable();  ExecutorService mExecutorService = Executors.newSingleThreadPool();  Future mfuture = mExecutorService.submit(mMyTestCallable);  try { System.out.println(mfuture.get());  } catch (Exception e) {  e.printStackTrace();} }  
      }

      此处runnable和Callable接口的区别主要体现在后者可以得到一个返回值  并可以抛出异常,而前者不可以。本示例是在一个线程池当中实现的该接口。利用

      newSingleThreadPool方法去创建一个线程池

转载于:https://www.cnblogs.com/zhangqiuchi/p/6659436.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416986.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git 大文件报错Out of memory, malloc failed、 The remote end hung up unexpectedly

git clone出现 "fatal: The remote end hung up unexpectedly" 设置 git config --global http.postBuffer 104857600000 后git clone报错 fatal: Out of memory, malloc failed (tried to allocate 1778384896 bytes) 又把postBuffer改了回去 git config --glo…

前端学习(2355):uni里面的样式学习

style.vue <template><view><view class"box">我是歌谣</view></view> </template><script>export default {data() {return {}},methods: {}} </script><style> .box{width: 375rpx;height: 375rpx;backgrou…

目前市场上的电脑一体机从计算机种类,一体机电脑与普通电脑的区别

现在&#xff0c;市场上开始流行一体机电脑了&#xff0c;很多网友可能会对一体机电脑感兴趣。下面&#xff0c;本文针对市场上的一体机电脑优劣作一个简要说明。一、一体机电脑的好处由于一体机电脑&#xff0c;所有的设备都封装在同一个容器内&#xff0c;就连显示屏都和电脑…

C#之调用存储过程

C#调用存储过程 以下内容可能有错漏之处&#xff0c;请大家多多指教。 C#后台代码如下&#xff1a; //调用存储过程的方法public static void Startupworkflow(string firstnodename, string secondnodename, string firstact) { SqlConnection conn new Sq…

css3修改透明png颜色

实现思路&#xff1a; 图片作为背景&#xff0c;drop-shadow阴影形成新的元素&#xff0c;再用left把整体偏移回去&#xff0c;最后用overflow:hidden把原图片隐藏 注&#xff1a;当放在其他绝对定位的元素上面时注意z-index的处理。 .iconXXX{overflow:hidden;display: inl…

计算机速录比赛主题,弘扬汉字,全国输入法表演赛电脑输入法比赛组速录表演组的报名...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼“弘扬汉字&#xff0c;全国输入法表演赛”组委会、评委、电脑输入法比赛组、速录表演组的报名现在已陆续报名&#xff0c;没有报名的赶快报名&#xff0c;有方案展示的也可以参加&#xff0c;没有填写详细信息的请及时填写&#x…

前端学习(2357):uni的基本数据绑定

<template><view><view>{{msg}}</view><view>{{你好世界}}</view></view> </template><script>export default {data() {return {msg:hello}},methods: {}} </script><style></style>运行结果

GitChat专栏:Spring Cloud 与 Consul 的整合使用

Spring Cloud 热度日益提升&#xff0c;注册中心、配置中心的选型是一个必然面对的问题。 Eureka 2.0 开源工作宣告停止&#xff0c;Zookeeper 略显笨重&#xff0c;Consul 是某种业务场景下相对较好的选择。Consul 部署简单&#xff0c;兼具注册中心和配置中心&#xff0c;Go …

地图收敛心得170405

寻路算法大总结! 交换机生成树采用的是完全不同的D-V(distance vector)距离矢量算法,并不是很可靠. 并不是任意两点之间的最短路径,因为任意两点之间取最短路径可能有环路:总权更大 交换机STP不一定是最小生成树!!!举例论证 因为它只是所有交换机到根桥最短 贪心算法的味道 kru…

win7下搭建小程序服务器,技术开发人员告诉你微信小程序怎么做

现在每个玩微信公众号的博主基本上都在玩微信小程序&#xff0c;现在的小程序特别火。虽然小程序特别火但也不是任何人想立马做就能做的&#xff0c;也是需要会开发及了解微信小程序的相关规则才能做的。今天邀请技术开发人员告诉你微信小程序怎么做&#xff0c;想入门的小伙伴…

前端学习(2358):v-bind和v-for

geyao.vue <template><view><view>{{msg}}</view><view>{{你好世界}}</view><image v-bind:src"imgUrl" mode""></image><view v-for"(item,index) in arr">序号:{{index}}名字:{{item.…

Java 操作 HDFS

HDFS 作为开源界比较成熟的分布式文件存储系统&#xff0c;适用于海量文件存储&#xff0c;本文介绍了如何使用 Java 操作 HDFS&#xff0c;采用 Maven 管理包。 pom.xml <dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client…

iOS---------- MBProgressHUD (1.0.0)的变动

1.改变菊花的颜色 // hud.color [UIColor blackColor];--------------> hud.bezelView.color [UIColor blackColor]; 2.改变菊花的坐标 // hud.yOffset-150; --------------> hud.offset CGPointMake(0, -150); 待续。。。 转载于:https:/…

怎么把数据文件上传云服务器,怎么把数据上传导云服务器

怎么把数据上传导云服务器 内容精选换一换安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。本地磁盘映射(推…

长路漫漫,唯剑作伴--loadView、viewDidLoad及viewDidUnload的关系

一、loadView 什么时候被调用&#xff1f; 每次访问UIViewController的view(比如controller.view、self.view)而且view为nil&#xff0c;loadView方法就会被调用。 有什么作用&#xff1f; loadView方法是用来负责创建UIViewController的view 默认实现是怎样的&#xff1f; 它会…

前端学习(2359):如何注册事件

<template><view><view>{{msg}}</view><view>{{你好世界}}</view><image v-bind:src"imgUrl" mode""></image><view v-for"(item,index) in arr">序号:{{index}}名字:{{item.name}},年龄…

hbase-client1.2.0新版api获取数据方式

public static void QueryAll(String tableName) {try {Table table connection.getTable(TableName.valueOf(tableName));ResultScanner rs table.getScanner(new Scan());for (Result r : rs) {System.out.println("获得到rowkey:" new String(r.getRow()));for…

微信无法连接服务器1-502,只有一部iphone x手机,在微信公众号中选择菜单,出现bad gateway 502错误,原因?...

1.菜单项对应的网页链接参见下图&#xff1a;2.后台看到下面的错误信息:--- iphone x 访问日志172.17.0.28 - - [03/Jan/2020:10:19:17 0800] "GET /m02?atwxyes HTTP/1.0" 502 182 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) Ap…

前端学习(2360):生命周期函数的学习

app.vue <script>export default {onLaunch: function() {console.log(App Launch)},onShow: function() {console.log(App Show)},onHide: function() {console.log(App Hide)},onError:function(err){console.log(出现异常,err)}} </script><style>/*每个…