package thread;
public class InterruptTest extends Thread
{
static int result =0 ;
public InterruptTest(String name ){ super(name);}
public static void main(String[] args)
{
System.out.println("主线程执行");
Thread t = new InterruptTest("计算子线程");
t.start();
System.out.println("初始值 result:"+ result);
try{
long start = System.nanoTime();
System.out.println("start time:"+ start);
t.join(10);
long end = System.nanoTime();
System.out.println("end time:"+ end);
t.interrupt();
System.out.println((end - start )/1+ "ms后:"+ result);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void run(){
System.out.println(this.getName()+" 开始计算");
for(int i =0 ; i<100000 ; i++)
{
result++;
if(Thread.interrupted()){
System.out.println(this.getName()+" 被中断执行了!");
return ;
}
}
System.out.println(this.getName()+" 结束计算");
}
}
----------------
主线程执行
初始值 result:0
计算子线程 开始计算
start time:15749702117102
end time:15749706104099
计算子线程 被中断执行了!
3986997ms后:36253
==================================
package thread;
public class InterruptTest extends Thread
{
public void run(){
while(true){
if(Thread.currentThread().isInterrupted()){
System.out.println("Someone interrupted me.but I still work .");
}
else{
System.out.println("this Thread is keeping working.");
}
}
}
public static void main(String[] args) throws InterruptedException {
InterruptTest tt = new InterruptTest();
tt.start();
Thread.sleep(6000);
tt.interrupt(); //只是修改了中断状态,线程并没有停止运行
}
}
==================================
http://www.mamicode.com/info-detail-517008.html 综合案例 jing dian 经典
http://blog.csdn.net/superit401/article/details/51243857
http://blog.csdn.net/evankaka/article/details/55808628 线程生产者 消费者
==================================
wait notify : 测试代码:
package threadwatinofify;
public class ThreadWaitNotify
{
public static void main(String[] args)
{
final Object obj = new Object();
Thread t1 = new Thread() {
public void run()
{
synchronized (obj) {
System.out.println("T1 start!");
try {
obj.wait();// 释放 锁 ,让出自己的锁 ,供其它线程执行 此代码块 ,自身被阻塞了,之后的代码不再执行了
// 直到 自己被 通知,才继续执行下面的代码
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T1 end!");
}
}
};
Thread t2 = new Thread() {
public void run()
{
synchronized (obj) {
System.out.println("T2 start!");
obj.notify();//线程自身继续执行,没有被阻塞。
System.out.println("T2 end!");
}
}
};
t1.start();
t2.start();
}
}
==================================
package threadExtends;
//父类 线程 , 让 各个子线程 继承 父类线程 , 不同的子线程 做同一件事情, 但是算法不同哦。
public class Father implements Runnable
{
String money ;
public Father( String money ){
// super();
this.money = money ;
}
public void output(){} //各个子线程要实现的 某种功能
public void run(){
//while(true)
output();
}
}
-------------------------------------------------------------------------------------------------------
package threadExtends;
public class Son1 extends Father
{
public Son1( String money ){
super( money );
}
public void output(){
try{
output(this.money);
}catch(Exception e){
e.printStackTrace();
}
}
public void output(String money){
System.out.println(money);
}
}
---------------------------------------------------------------------------------------------------------
package threadExtends;
public class Son2 extends Father
{
public Son2( String money ){
super( money );
}
public void output(){
output(this.money);
}
public void output(String money){
System.out.println(money);
}
}
---------------------------------------------------------------------------------------------------
package threadExtends;
public class Test
{
public static void main(String[] args)
{
Son1 s1 = new Son1("10元");
Son1 s2 = new Son1("2000元");
Thread t1 = new Thread( s1 );
Thread t2 = new Thread( s2 );
t1.start();
t2.start();
}
}
------------------------
10元
2000元
===============================================================
package package1;
//利用此类创建3个子线程 ,每个子线程执行一种算法 实现排序
public class Sort implements Runnable{
int []data;
public Sort( int[] data){
super();
this.data=data;
}
public void run(){
long start = System.nanoTime();
sort();
long end = System.nanoTime();
System.out.println( Thread.currentThread().getName() + "执行时间:" +( end - start )+"ns");
}
public void sort( ){
throw new RuntimeException("please do it in subclass");
}
}
-----------------------------------------------------------
package package1;
public class BBSort extends Sort {
public BBSort(int[] data){
super(data);
}
public void sort(){
try{
sort(data);
}catch(Exception e){
e.printStackTrace();
}
}
void sort(int a[]) throws Exception{
int j;
int limit = a.length;
int st = -1;
while(st<limit){
st++;
limit--;
boolean swapped=false;
for(j= st ; j<limit;j++){
if(a[j]>a[j+1])
{
int T = a[j];
a[j]=a[j+1];
a[j+1]=T;
swapped = true;
}
}
if(!swapped){
return ;
}else
swapped = false;
for( j = limit; --j >= st;){
if(a[j]>a[j+1]){
int T = a[j];
a[j]=a[j+1];
a[j+1]=T;
swapped = true;
}
}
if(!swapped){
return ;
}
}
}
}
----------------------------------------------------------------------
package package1;
public class MainTest {
public static void main(String[] args) {
final int count = 1000;
int[] data = createData(count);
Thread bbsort = new Thread( new BBSort(data),"双向冒泡排序");
bbsort.start();
}
private static int[] createData(int count){
int[] data= new int[count];
for(int i = 0 ; i< data.length;i++){
data[i]=(int)(Math.random()*count);
}
return data;
}
}
===============================================
==================================
==================================
==================================
==================================
==================================
==================================
==================================
==================================
==================================