对线程了解不多,且不经常去用,最近想起,小小了解,且做个小demon和大家分享一下。
demon:两个线程,分别交替打印20次后中断。
package com.cn.ld.exercises;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Administrator
* Exercise
* 2011-9-22 下午04:55:55
* Description:
*/
public class Test {
private static AtomicInteger s_count = new AtomicInteger(0);
private static boolean stop ;
private static int n = 0 ;
private static int n1 = 0;
private static int n2 = 0;
public Test(boolean stop){
this.stop = stop ;
}
public class thread1 implements Runnable {
public boolean stop= false ;
public thread1(boolean stop){
this.stop = stop ;
}
public boolean getStop(){
return stop ;
}
public void run() {
synchronized (s_count) {
while (!stop) {
if(n2==21&&n1==21){
try {
Thread.sleep(10000);
stop = true ;
Thread.interrupted();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (n % 2 != 0) {
System.out.println(Thread.currentThread().getName()
+ ":运行了" + n1 + "次!");
n++ ;
n1++;
s_count.notify();
} else {
try {
s_count.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
public class thread2 implements Runnable {
public boolean stop= false ;
public thread2(boolean stop){
this.stop = stop ;
}
public boolean getStop(){
return stop ;
}
public void run() {
synchronized (s_count) {
while (!stop) {
if(n2==21&&n1==21){
try {
Thread.sleep(10000);
stop = true ;
Thread.interrupted();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (n % 2 == 0) {
System.out.println(Thread.currentThread().getName()
+ ":运行了" + n2 + "次!");
n++;
n2++;
s_count.notify();
} else {
try {
s_count.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Test test = new Test(false);
Thread t1 = new Thread(test.new thread1(test.stop));
Thread t2 = new Thread(test.new thread2(test.stop));
t1.start();
t2.start();
}
}
写的很丑陋,还请见谅,大家尽可怕转
分享到:
相关推荐
Thread thread1 = new Thread(new ThreadStart( Count)); 其中的 Count 是将要被新线程执行的函数。 b.杀死线程 “杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着...
c++多线程编程初探 HANDLE thread1; HANDLE thread2; thread1=CreateThread(NULL,0,Fun1,NULL,0,NULL); thread2=CreateThread(NULL,0,Fun2,NULL,0,NULL);
上一篇博客《C++11 并发指南一(C++11 多线程初探)》中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用法。 std::thread 在 <thread> 头文件中声明,因此使用 ...
了解异步之前,我们先来看看Thread对象的升级版本Task对象: 1、Task对象的前世今生:Task对象是.Net Framework 4.0之后出现的异步编程的一个重要对象。在一定程度上来说,Task对象可以理解Thread对象的一个...
相信 Linux 程序员都用过 Pthread, 但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++11 的多线程...
通用机密计算平台技术初探 Python在大数据领域实践和思考 RT-Thread Smart生态之路 云计算与操作系统产业的发展 LAXCUS分布式操作系统-从0到1的实践 Mixed-Criticality System ,Scheduling in seL4 ...
OAuth2.0初探 学习编写 REST 接口测试用例 学习自定义 validator 学习自定义 filter 学习自定义 interceptor 自定义 aspect 学习 REST 接口多线程编程 Thread#start Thread#stop spring boot 1.5.20.RELEASE spring ...
1.4 初探Hello World 14 1.4.1 类(Class):Java世界中一类物体 14 1.4.2 方法(Method):物体的功能 15 1.4.3 main()方法:所有Java程序执行的起点 15 .1.5 名词解释 16 1.5.1 JDK和Java平台 16 1.5.2 ...
1.4 初探Hello World 14 1.4.1 类(Class):Java世界中一类物体 14 1.4.2 方法(Method):物体的功能 15 1.4.3 main()方法:所有Java程序执行的起点 15 .1.5 名词解释 16 1.5.1 JDK和Java平台 16 1.5.2 ...
│ ├55 - 线程_Thread1.avi │ ├56 - 线程_Thread2.mp4 │ ├57 - 线程_Thread3.mp4 │ ├58 - 网络编程_Concept1.mp4 │ ├59 - 网络编程_Concept2.avi │ ├60 - 网络编程_Concept3.mp4 │ ├61 - 网络编程_...
9.86、ANDROID自动化测试初探 324 9.86.1、捕获Activity上的Element 324 9.86.2、Hierarchyviewer 捕获Element的 328 9.86.3、架构实现 330 9.86.4、模拟键盘鼠标事件(Socket+Instrumentation实现) 332 9.86.5、再...
9.86、ANDROID自动化测试初探 324 9.86.1、捕获Activity上的Element 324 9.86.2、Hierarchyviewer 捕获Element的 328 9.86.3、架构实现 330 9.86.4、模拟键盘鼠标事件(Socket+Instrumentation实现) 332 9.86.5、再...