码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA线程学习(一)

时间:2014-12-20 20:58:40      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

package com;


public class Actor extends Thread{
private int count;
//运行的方法
public void run(){
System.out.println(getName() + "是一个演员");
System.out.println(getName() + "登台演出" + (++count));
System.out.println(getName() + "演出结束");
}


//观察调用了多少次run方法。
public static void main(String[] args){
Actor ac = new Actor();
ac.setName("Mr Thread");
ac.start();

}

}



//=================================================================//

无限循环的



package com;


public class Actor extends Thread{
private int count;
private boolean keepRunning = true;
//运行的方法
public void run(){
System.out.println(getName() + "是一个演员");
while(keepRunning)
{
System.out.println(getName() + "登台演出" + (++count));
}
System.out.println(getName() + "演出结束");
}


//观察调用了多少次run方法。
public static void main(String[] args){
Actor ac = new Actor();
ac.setName("Mr Thread");
ac.start();

}

}




//====================================================//

加了判断的100次



package com;


public class Actor extends Thread{
private int count;
private boolean keepRunning = true;
//运行的方法
public void run(){
System.out.println(getName() + "是一个演员");
while(keepRunning)
{
System.out.println(getName() + "登台演出" + (++count));
if(count == 100)
{

keepRunning = false;
}
}
System.out.println(getName() + "演出结束");
}


//观察调用了多少次run方法。
public static void main(String[] args){
Actor ac = new Actor();
ac.setName("Mr Thread");
ac.start();

}

}



//===============================================//

加上休眠

每十次休息一秒钟



package com;


public class Actor extends Thread{
private int count;
private boolean keepRunning = true;
//运行的方法
public void run(){
System.out.println(getName() + "是一个演员");
while(keepRunning)
{
System.out.println(getName() + "登台演出" + (++count));
if(count == 100)
{

keepRunning = false;
}
if(count %10 == 0)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
System.out.println(getName() + "演出结束");
}


//观察调用了多少次run方法。
public static void main(String[] args){
Actor ac = new Actor();
ac.setName("Mr Thread");
ac.start();

}

}



//=================================================//

实现runnable接口

两个线程交替实现

package com;


public class Actor extends Thread{
private int count;
private boolean keepRunning = true;
//运行的方法
public void run(){
System.out.println(getName() + "是一个演员");
while(keepRunning)
{
System.out.println(getName() + "登台演出" + (++count));
if(count == 100)
{

keepRunning = false;
}
if(count %10 == 0)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
System.out.println(getName() + "演出结束");
}


//观察调用了多少次run方法。
public static void main(String[] args){
Actor ac = new Actor();
ac.setName("Mr Thread");
ac.start();

Thread actressThread = new Thread(new Actress(), "Ms Runnable");
actressThread.start();

}

}




class Actress implements Runnable{


private int count;
private boolean keepRunning = true;
//运行的方法
public void run(){
System.out.println(Thread.currentThread().getName() + "是一个演员");
while(keepRunning)
{
System.out.println(Thread.currentThread().getName() + "登台演出" + (++count));
if(count == 100)
{

keepRunning = false;
}
if(count %10 == 0)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
System.out.println(Thread.currentThread().getName() + "演出结束");
}




}

JAVA线程学习(一)

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/42044975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!