java线程写法
来源:互联网 发布:皮带什么牌子好 知乎 编辑:程序博客网 时间:2024/04/29 05:29
//线程创建方式1
Thread thread=new Thread(){
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
}
};
thread.start();
//线程创建方式2
Thread thread2=new Thread(new Runnable(){
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
}
});
thread2.start();
//main程序
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
Thread thread=new Thread(){
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
}
};
thread.start();
//线程创建方式2
Thread thread2=new Thread(new Runnable(){
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
}
});
thread2.start();
//main程序
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
}
-------------------------------------------------------------------------------------------------
//混合
new Thread(new Runnable(){
public void run() {
System.out.println("Override runnable run!");
}
}){
public void run() {
//super.run();
System.out.println("Override Thread run!");
}
}.start();
执行结果Override Thread run!
如果加上super.run();结果为:
Override runnable run!
Override Thread run!
0 0
- java线程写法
- java线程写法
- java线程池标准写法
- Java线程_(一)线程的写法
- 线程写法
- JAVA单例类的线程安全最简洁有效的写法
- JAVA单例类的线程安全最简洁有效的写法
- Java学习之线程锁--单例模式写法--synchronized
- Java学习之线程锁--单例模式写法--synchronized
- 线程Callable写法
- java面试题----用java代码写死锁(线程join写法)
- vb.net 线程委托写法
- 线程安全的队列写法
- java编程之线程,继承Thread类,匿名内部类的写法
- java编程之线程,实现Runnable接口,匿名内部类的写法
- ConsoleReader.java的写法
- Java Mail写法
- MD5的java写法
- 文字自动换行(一个单词过长时ssssssssssssss)
- redis配置文件详解
- nyoj54小明的存钱计划
- UIImage的scale是什么?
- java 复习题
- java线程写法
- Prime Path(bfs)
- 关于CGRect的知识
- LED阵列PCB灯板绘制
- 模块SEO优化中{分类名称}分隔符去掉及只调用下级分类方法
- HDU 1506 dp虐的不是你的人,是你的脑子。
- [bug小记]System.loadLibrary加载so库时报警告JNI_OnLoad returned bad version(-1)并且crash
- Unix:关于使用vi
- tarjan + 缩点