package com.muzeet.mutithread;
//每个任务都是Runable接口的一个实例,任务是可运行对象,线程是便于任务执行的对象。必须创建任务类,重写run方法定义任务
public class ThreadDemo1 implements Runnable {
private int countDown = 10;
@Override
//重写run方法,定义任务
public void run() {
while(countDown-- >0)
{
System.out.println("$" + Thread.currentThread().getName()
+ "(" + countDown + ")");
}
}
//调用start方法会启动一个线程,导致任务中的run方法被调用,run方法执行完毕则线程终止
public static void main(String[] args) {
Runnable demo1 = new ThreadDemo1();
Thread thread1 = new Thread(demo1);
Thread thread2 = new Thread(demo1);
thread1.start();
thread2.start();
System.out.println("火箭发射倒计时:");
}
}
火箭发射倒计时: $Thread-0(9) $Thread-0(8) $Thread-0(7) $Thread-0(6) $Thread-0(5) $Thread-0(4) $Thread-0(3) $Thread-0(2) $Thread-0(1) $Thread-0(0)
public static void main(String[] args) {
Runnable demo1 = new ThreadDemo1();
Runnable demo2 = new ThreadDemo1();
Thread thread1 = new Thread(demo1);
Thread thread2 = new Thread(demo2);
thread1.start();
thread2.start();
System.out.println("火箭发射倒计时:");
}
火箭发射倒计时: $Thread-0(9) $Thread-0(8) $Thread-0(7) $Thread-0(6) $Thread-1(9) $Thread-0(5) $Thread-1(8) $Thread-0(4) $Thread-1(7) $Thread-0(3) $Thread-1(6) $Thread-1(5) $Thread-0(2) $Thread-1(4) $Thread-1(3) $Thread-1(2) $Thread-1(1) $Thread-1(0) $Thread-0(1) $Thread-0(0)
package com.muzeet.mutithread;
//每个任务都是Runable接口的一个实例,任务是可运行对象,线程即可运行对象。必须创建任务类,重写run方法定义任务
public class ExtendFromThread extends Thread {
private int countDown = 10;
@Override
//重写run方法,定义任务
public void run() {
while(countDown-- >0)
{
System.out.println("$" + this.getName()
+ "(" + countDown + ")");
}
}
//调用start方法会启动一个线程,导致任务中的run方法被调用,run方法执行完毕则线程终止
public static void main(String[] args) {
ExtendFromThread thread1 = new ExtendFromThread();
ExtendFromThread thread2 = new ExtendFromThread();
thread1.start();
thread2.start();
System.out.println("火箭发射倒计时:");
}
}
火箭发射倒计时: $Thread-0(9) $Thread-0(8) $Thread-0(7) $Thread-0(6) $Thread-0(5) $Thread-0(4) $Thread-0(3) $Thread-0(2) $Thread-0(1) $Thread-0(0) $Thread-1(9) $Thread-1(8) $Thread-1(7) $Thread-1(6) $Thread-1(5) $Thread-1(4) $Thread-1(3) $Thread-1(2) $Thread-1(1) $Thread-1(0)
package com.muzeet.testThread;
public class PrintNum implements Runnable {
private int lastNum;
public PrintNum(int n)
{
lastNum = n;
}
@Override
public void run() {
// TODO Auto-generated method stub
Thread thread4 = new Thread(new PrintChar('c', 40));
thread4.start();
try {
for(int i=1;i<=lastNum;i++)
{
System.out.println(" " + i);
if(i == 50)
{
thread4.join();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有