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基础(四):线程的创建与执行的更多相关文章

  1. 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep

    /** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...

  2. 重学JAVA基础(八):锁的基本知识

    1.线程状态 如上图,当我们新建一个线程,并start后,其实不一定会马上执行,因为只有操作系统调度了我们的线程,才能真正进行执行,而操作系统也随时可以运行其他线程,这时线程又回到可运行状态.这个过程 ...

  3. Java基础-多线程-①线程的创建和启动

    简单阐释进程和线程 对于进程最直观的感受应该就是“windows任务管理器”中的进程管理: (计算机原理课上的记忆已经快要模糊了,简单理解一下):一个进程就是一个“执行中的程序”,是程序在计算机上的一 ...

  4. 重学JAVA基础(二):Java反射

        看一下百度的解释:       JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息     ...

  5. 重学JAVA基础(六):多线程的同步

    1.synchronized关键字 /** * 同步关键字 * @author tomsnail * @date 2015年4月18日 下午12:12:39 */ public class SyncT ...

  6. 重学JAVA基础(五):面向对象

    1.封装 import java.util.Date; public class Human { protected String name; protected BirthDay birthDay; ...

  7. 重学JAVA基础(一):PATH和CLASSPATH

    我想大多数Java初学者都会遇到的问题,那就是怎么配置环境,执行java -jar xxx.jar  都会报NoClassDefFindError,我在最开始学习的时候,也遇到了这些问题. 1.PAT ...

  8. 重学JAVA基础(三):动态代理

    1.接口 public interface Hello { public void sayHello(); } 2.实例类 public class Hello2 { public void sayH ...

  9. 重学 Java 设计模式:实战抽象工厂模式

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!

随机推荐

  1. Hibernate demo之使用注解

    1.新建maven项目 testHibernate,pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  2. iOS开发系列--让你的应用“动”起来【转载】

    概览 原文链接:http://www.cnblogs.com/kenshincui/p/3972100.html 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...

  3. HDFS源码分析之EditLogTailer

    在FSNamesystem中,有这么一个成员变量,定义如下: /** * Used when this NN is in standby state to read from the shared e ...

  4. 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window

    当编写自动化脚本,定位浏览器元素时,报如下错误: 代码: >>> # coding=utf-8 >>> from selenium import webdriver ...

  5. nginx教程2:日志

    主要有两种:access_log(访问日志) 和 error_log(错误日志). access_log 访问日志 access_log 主要记录客户端访问 Nginx 的每一个请求,格式可以自定义. ...

  6. x86 的 TSS 任务切换机制

    转自:http://blog.chinaunix.net/uid-587665-id-2732907.html [0]写在前面 segment descriptors 构建保护模式下的最基本.最根本的 ...

  7. android shareSDK 微博分享案例

    android shareSDK 微博分享案例 ShareSDK APP_KEY 219b1121fc68 腾讯微博 key 801517904 secret bfba83ae253c8f38dabe ...

  8. DataGrid绑定Dictionary问题

    问题] 在最近的项目中使用DataGrid的DataGridCheckBoxColumn绑定了后台TagModel类的IsSelected字段,数据源是TagModel类型的Dictionary,运行 ...

  9. BZOJ 1597: [Usaco2008 Mar]土地购买 斜率优化

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MB Description 农夫John准备扩大他的农场,他正在考虑N ...

  10. wait() 区别 sleep()

    wait() notify() notifyAll() wait和notify方法必须写在synchronized方法内,即在调用wait和notify方法前,需先获得对象锁: 调用wait方法则释放 ...