线程的实现
来源:互联网 发布:c语言continue在if中 编辑:程序博客网 时间:2024/06/05 21:04
public class Runnabl {
public static void main(String[] args) {
MyRunnable1 st = new MyRunnable1();
Thread t1 = new Thread(st, "1号窗口");
Thread t2 = new Thread(st, "2号窗口");
Thread t3 = new Thread(st, "3号窗口");
Thread t4 = new Thread(st, "4号窗口 ");
t1.setPriority(7);
t2.setPriority(8);
t3.setPriority(9);
t4.setPriority(10);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
============================================
package cn.yq.java.xiancheng2;
public class MyRunnable1 implements Runnable {
int tickets = 500;
public void run(){
while(tickets > 0){
sale();
}
}
private synchronized void sale(){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() +"\t卖出 第 "+ (500 - tickets + 1)+"张票");
tickets--;
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
================================================
package cn.yq.java.xiancheng2;
public class Threa1 {
public static void main(String[] args) {
MyThread1 myth1=new MyThread1("Myth-01");
MyThread1 myth2=new MyThread1("Myth-02");
MyThread1 myth3=new MyThread1("Myth-03");
MyThread1 myth4=new MyThread1("Myth-04");
MyThread1 myth5=new MyThread1("Myth-05");
MyThread1 myth6=new MyThread1("Myth-06");
//同步 随机 (myth1.start报错)
myth1.start();
myth2.start();
//睡眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myth3.start();
myth4.start();
myth5.start();
myth6.start();
}
}
=================================================
package cn.yq.java.xiancheng2;
public class MyThread1 extends Thread {
String name;
int age= 10;
MyThread1(String name){
this.name = name;
}
public void run(){
for(int i = 0;i<20;i++){
if(age>0){
System.out.println("继承Thread的线程:"+Thread.currentThread().getName()+"\tage:"+age--);
}
}
}
}
- JAVA线程-线程的实现
- Android线程、线程、线程的实现方式!
- 线程的实现
- 线程的实现
- 线程的实现
- 远程线程的实现
- 线程池的实现
- 线程的简单实现
- 线程池的实现
- 隐藏的线程实现
- 线程池的实现
- 线程的实现细节
- 线程的实现
- linux 线程的实现
- 线程的实现模型
- 线程的实现方式
- 线程池的实现
- 线程的实现一
- PHP5.6.30配置Mysql和mantis
- 两种实现 无头结点单向链表 的插入、查找、删除
- 列表生成式
- 【Python】产生随机数字09
- python win7 64位下无法安装mysql驱动问题
- 线程的实现
- 有上下界的网络流小结
- 静态链接、动态链接、静态重定位、动态重定位
- 往JScrollPane中添加组件时滚动条不出现的解决方法
- mysqld_multi配置多实例
- C++面试题之二叉树
- 漫步最优化十一——局部极小与极大的充分必要条件(上)
- 【Natural Language Processing】基于CRF++的中文分词
- 三种帧的说明