重学JAVA基础(四):线程的创建与执行
1.继承Thread
public class TestThread extends Thread{ public void run(){
System.out.println(Thread.currentThread().getName());
} public static void main(String[] args) {
Thread t = new TestThread();
t.start();
}
}
2.实现Runnable
public class TestRunnable implements Runnable
{ @Override
public void run() {
System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) {
Thread t = new Thread(new TestRunnable());
t.start();
}
}
3.线程池
public class TestThreadPool { public static ExecutorService singlePool = Executors.newSingleThreadExecutor(); private static ExecutorService fixedPool = Executors.newFixedThreadPool(2); private static ExecutorService cachedPool = Executors.newCachedThreadPool(); private static ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new DefaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy()); private static class DefaultThreadFactory implements ThreadFactory{
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
return t;
}
} public static void main(String[] args) {
singlePool.execute(new TestThreadP());
fixedPool.execute(new TestThreadP());
cachedPool.execute(new TestThreadP());
tpe.execute(new TestThreadP());
} }
class TestThreadP implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
} }
重学JAVA基础(四):线程的创建与执行的更多相关文章
- 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep
/** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...
- 重学JAVA基础(八):锁的基本知识
1.线程状态 如上图,当我们新建一个线程,并start后,其实不一定会马上执行,因为只有操作系统调度了我们的线程,才能真正进行执行,而操作系统也随时可以运行其他线程,这时线程又回到可运行状态.这个过程 ...
- Java基础-多线程-①线程的创建和启动
简单阐释进程和线程 对于进程最直观的感受应该就是“windows任务管理器”中的进程管理: (计算机原理课上的记忆已经快要模糊了,简单理解一下):一个进程就是一个“执行中的程序”,是程序在计算机上的一 ...
- 重学JAVA基础(二):Java反射
看一下百度的解释: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息 ...
- 重学JAVA基础(六):多线程的同步
1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...
- 重学JAVA基础(五):面向对象
1.封装 import java.util.Date; public class Human { protected String name; protected BirthDay birthDay; ...
- 重学JAVA基础(一):PATH和CLASSPATH
我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar 都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...
- 重学JAVA基础(三):动态代理
1.接口 public interface Hello { public void sayHello(); } 2.实例类 public class Hello2 { public void sayH ...
- 重学 Java 设计模式:实战抽象工厂模式
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的. 所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可以完 ...
- 01 http协议概念及工作流程
一:HTTP协议 重要性: 无论是以后用webserverice ,还是用rest做大型架构,都离不开对HTTP协议的认识. 甚至可以简化的说: webservice = http协议+XML Res ...
- js jquery 插件
$(function(){ (function($, document, undefiend){ $.fn.pagination = function(options){ var $this = $( ...
- Android 红色小圆球提示气泡 BadgeView
今天给大家分享两个实用有简单的一个小圆球提示气泡: BadgeView 参考地址: https://github.com/qstumn/BadgeView; 个人地址:http://git ...
- BeeFramework 系列二 UISignal篇下
本文转载至 http://www.apkbus.com/android-126129-1-1.html qihoo2 该用户从未签到 156 主题 156 帖子 1826 积分 Android ...
- Large-scale Incremental Processing Using Distributed Transactions and Notifications
Large-scale Incremental Processing Using Distributed Transactions and Notifications
- php xmlrpc使用示例
xmlrpc 远程过程调用, 使用xml文本方式传输数据. soap协议比xmlrpc复杂并强大. 1.修改 php.ini,开启 xmlrpc 扩展 2.rpc_client.php <?ph ...
- 【题解】DZY Loves Chinese
[题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...
- TensorFlow Action(开山使用篇)
1.TensorFlow安装: 使用pip install tensorflow安装CPU版: 或使用pip install tensorflow-gpu==1.2.1指定版本安装GPU版. 2.Te ...
- Python爬虫 —— 抓取美女图片(Scrapy篇)
杂谈: 之前用requests模块爬取了美女图片,今天用scrapy框架实现了一遍. (图片尺度确实大了点,但老衲早已无恋红尘,权当观赏哈哈哈) Item: # -*- coding: utf-8 - ...