首页 > 代码库 > JAVA线程学习(一)
JAVA线程学习(一)
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线程学习(一)