创建线程
来源:互联网 发布:linux mib库在哪 编辑:程序博客网 时间:2024/06/08 04:45
public class SimpleThread extends Thread{
private int countdown=5;//声明并赋值countdown=5
private int threadNumber;//声明变量threadNumber
private static int threadCount=0;//声明全局变量threadCount=0
public SimpleThread(){
threadNumber=++threadCount;//threadCount+1后再赋值给threadNumber
System.out.println("Making"+threadNumber);//打印
}
public void run(){
while(true){System.out.println("Thread"+threadNumber+"("+countdown+")");//如果为true,打印
if(--countdown==0)return;}//如果countdown-1为0,返回
}
public static void main(String[] args) {
for(int i=0;i<=5;i++)
{new SimpleThread().start();
System.out.println("All Thread started");
}
}
private int countdown=5;//声明并赋值countdown=5
private int threadNumber;//声明变量threadNumber
private static int threadCount=0;//声明全局变量threadCount=0
public SimpleThread(){
threadNumber=++threadCount;//threadCount+1后再赋值给threadNumber
System.out.println("Making"+threadNumber);//打印
}
public void run(){
while(true){System.out.println("Thread"+threadNumber+"("+countdown+")");//如果为true,打印
if(--countdown==0)return;}//如果countdown-1为0,返回
}
public static void main(String[] args) {
for(int i=0;i<=5;i++)
{new SimpleThread().start();
System.out.println("All Thread started");
}
}
}
每次输出都不同。
0 0
- 创建线程
- 创建线程
- 创建线程
- 线程创建
- 线程创建
- 创建线程
- 创建线程
- 创建线程
- 创建线程
- 线程创建
- 创建线程
- 线程创建
- 创建线程
- 创建线程
- 创建线程
- 线程创建
- 创建线程
- 线程创建
- str库函数模拟实现
- 1015. 德才论 (25)
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 异常:TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
- 创建线程
- 【Linux】screen 常用命令
- 【Linux】ubuntu系统权限chmod的使用
- expire
- mq
- pipeline
- 第二类曲面积分某问题
- 【宇宙无敌BUG】Tomcat服务器 修改Deploy Location
- Docker aufs存储驱动layer、diff、mnt目录的区别