Java学习笔记---------如何使用Runnable接口来创建线程???
来源:互联网 发布:淘宝上1元秒杀是真的吗 编辑:程序博客网 时间:2024/04/30 14:59
/*
* 如何使用接口来定义线程???
* 1.实现Runnable类
* 2.创建Runnable类对象
* 3.把Runnable类对象传递给thread(Runnable)构造一个thread的对象
* 4.调用thread的对象的start(),来启动线程。
*
*
*
*
* */
package com.thread;
public class Time
{
static class Time1 implements Runnable
{
public void run()
{
System.out.println("这是一个线程1");
}
Time1()
{
}
}
static class Time2 implements Runnable
{
public void run()
{
try{
Thread.sleep(10000);
System.out.println("这是一个线程2");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Time1 th=new Time1();
Time2 th1=new Time2();
Thread thr=new Thread(th);
Thread thr1=new Thread(th1);
try{
thr.start();
thr.sleep(1000);//在使用线程的地方,一定要有线程异常处理。
// Multiple markers at this line
// - The static method sleep(long) from the type Thread should be accessed in a
// static way
thr1.start();
// 这句:Time1 th=new Time1();报错:Multiple markers at this line
// - No enclosing instance of type Time is accessible. Must qualify the allocation with an enclosing instance of type Time (e.g. x.new A() where x is an instance of
// Time).
// - The local variable th is never read
}catch(Exception e)
{
e.printStackTrace();
}finally{
}
}
}
* 如何使用接口来定义线程???
* 1.实现Runnable类
* 2.创建Runnable类对象
* 3.把Runnable类对象传递给thread(Runnable)构造一个thread的对象
* 4.调用thread的对象的start(),来启动线程。
*
*
*
*
* */
package com.thread;
public class Time
{
static class Time1 implements Runnable
{
public void run()
{
System.out.println("这是一个线程1");
}
Time1()
{
}
}
static class Time2 implements Runnable
{
public void run()
{
try{
Thread.sleep(10000);
System.out.println("这是一个线程2");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Time1 th=new Time1();
Time2 th1=new Time2();
Thread thr=new Thread(th);
Thread thr1=new Thread(th1);
try{
thr.start();
thr.sleep(1000);//在使用线程的地方,一定要有线程异常处理。
// Multiple markers at this line
// - The static method sleep(long) from the type Thread should be accessed in a
// static way
thr1.start();
// 这句:Time1 th=new Time1();报错:Multiple markers at this line
// - No enclosing instance of type Time is accessible. Must qualify the allocation with an enclosing instance of type Time (e.g. x.new A() where x is an instance of
// Time).
// - The local variable th is never read
}catch(Exception e)
{
e.printStackTrace();
}finally{
}
}
}
0 0
- Java学习笔记---------如何使用Runnable接口来创建线程???
- JAVA学习笔记(三十八)- 创建实现Runnable接口的线程
- JAVA中通过实现Runnable接口来创建线程类
- Java通过实现Runnable接口来创建线程
- java多线程学习1-继承Thread类和实现Runnable接口来创建线程
- Java线程入门2:使用Runnable接口创建线程
- Java线程3:使用Runnable接口创建线程
- java基础学习总结——线程之使用Runnable接口创建线程的方法
- App11_02_利用Runnable接口来创建线程
- 通过实现Runnable接口来创建线程
- 3. 初学Java多线程:使用Runnable接口创建线程
- 初学Java多线程【3】:使用Runnable接口创建线程
- 初学Java多线程:使用Runnable接口创建线程
- 初学Java多线程:使用Runnable接口创建线程
- 三、初学Java多线程:使用Runnable接口创建线程
- Java 创建线程 实现Runnable接口
- 实现Runnable接口的类+使用Thread类的实例来创建线程---->通过实现Runnable接口来创建线程类
- 《黑马程序员》java笔记->售票例子实现Runnable接口创建线程
- Android访问权限大全
- 算法竞赛入门经典高精度运算推荐题目
- log4j 日志详解
- Codeforces 560B Gerald is into Art(构造性算法)
- Json lib 抛出 NestableRuntimeException
- Java学习笔记---------如何使用Runnable接口来创建线程???
- java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.ValueStack
- python 自己写爬虫 ----- BeautifulSoup
- Eclipse免reload设置
- POJ 1961 && HDU 1358 Period(kmp)
- cuda7.0+ caffe 小白安装手记
- hdu 2838 树状数组
- Android Studio 活动的最佳实践 知晓当前是在哪一个活动 随时随地地进入程序 启动活动的最佳写法 第一行代码
- 基于Spark构建推荐引擎