public Thread( ); public Thread(Runnable target); public Thread(String name); public Thread(Runnable target, String name); public Thread(ThreadGroup group, Runnable target); public Thread(ThreadGroup group, String name); public Thread(ThreadGroup group, Runnable target, String name); public Thread(ThreadGroup group, Runnable target, String name, long stackSize);
public class ThreadDemo1 {
public static void main(String[] args){
Demo d = new Demo();
d.start();
for(int i=0;i<60;i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
class Demo extends Thread{
public void run(){
for(int i=0;i<60;i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
public class ThreadDemo2 {
public static void main(String[] args){
Demo2 d =new Demo2();
Thread t = new Thread(d);
t.start();
for(int x=0;x<60;x++){
System.out.println(Thread.currentThread().getName()+x);
}
}
}
class Demo2 implements Runnable{
public void run(){
for(int x=0;x<60;x++){
System.out.println(Thread.currentThread().getName()+x);
}
}
}
// 开始线程 publicvoid start( ); publicvoid run( ); // 挂起和唤醒线程 publicvoid resume( ); // 不建议使用 publicvoid suspend( ); // 不建议使用 publicstaticvoid sleep(long millis); publicstaticvoid sleep(long millis, int nanos); // 终止线程 publicvoid stop( ); // 不建议使用 publicvoid interrupt( ); // 得到线程状态 publicboolean isAlive( ); publicboolean isInterrupted( ); publicstaticboolean interrupted( ); // join方法 publicvoid join( ) throws InterruptedException;
publicstaticvoid sleep(long millis) throws InterruptedException publicstaticvoid sleep(long millis, int nanos) throws InterruptedException
public class ThreadDemo3 {
public static void main(String[] args){
Ticket t =new Ticket();
Thread t1 = new Thread(t,"窗口一");
Thread t2 = new Thread(t,"窗口二");
Thread t3 = new Thread(t,"窗口三");
Thread t4 = new Thread(t,"窗口四");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable{
private int ticket =400;
public void run(){
while(true){
synchronized (new Object()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ticket<=0)
break;
System.out.println(Thread.currentThread().getName()+"---卖出"+ticket--);
}
}
}
}
public class ThreadDemo3 {
public static void main(String[] args){
Ticket t =new Ticket();
Thread t1 = new Thread(t,"窗口一");
Thread t2 = new Thread(t,"窗口二");
Thread t3 = new Thread(t,"窗口三");
Thread t4 = new Thread(t,"窗口四");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable{
private int ticket = 4000;
public synchronized void saleTicket(){
if(ticket>0)
System.out.println(Thread.currentThread().getName()+"卖出了"+ticket--);
}
public void run(){
while(true){
saleTicket();
}
}
}
public class ThreadDemo3 {
public static void main(String[] args){
class Person{
public String name;
private String gender;
public void set(String name,String gender){
this.name =name;
this.gender =gender;
}
public void get(){
System.out.println(this.name+"...."+this.gender);
}
}
final Person p =new Person();
new Thread(new Runnable(){
public void run(){
int x=0;
while(true){
if(x==0){
p.set("张三", "男");
}else{
p.set("lili", "nv");
}
x=(x+1)%2;
}
}
}).start();
new Thread(new Runnable(){
public void run(){
while(true){
p.get();
}
}
}).start();
}
}
/*
张三....男
张三....男
lili....nv
lili....男
张三....nv
lili....男
*/
public class ThreadDemo3 {
public static void main(String[] args){
class Person{
public String name;
private String gender;
public void set(String name,String gender){
this.name =name;
this.gender =gender;
}
public void get(){
System.out.println(this.name+"...."+this.gender);
}
}
final Person p =new Person();
new Thread(new Runnable(){
public void run(){
int x=0;
while(true){
synchronized (p) {
if(x==0){
p.set("张三", "男");
}else{
p.set("lili", "nv");
}
x=(x+1)%2;
}
}
}
}).start();
new Thread(new Runnable(){
public void run(){
while(true){
synchronized (p) {
p.get();
}
}
}
}).start();
}
}
/*
lili....nv
lili....nv
lili....nv
lili....nv
lili....nv
lili....nv
张三....男
张三....男
张三....男
张三....男
*/
/*
*线程等待唤醒机制
*等待和唤醒必须是同一把锁
*/
public class ThreadDemo3 {
private static boolean flags =false;
public static void main(String[] args){
class Person{
public String name;
private String gender;
public void set(String name,String gender){
this.name =name;
this.gender =gender;
}
public void get(){
System.out.println(this.name+"...."+this.gender);
}
}
final Person p =new Person();
new Thread(new Runnable(){
public void run(){
int x=0;
while(true){
synchronized (p) {
if(flags)
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
if(x==0){
p.set("张三", "男");
}else{
p.set("lili", "nv");
}
x=(x+1)%2;
flags =true;
p.notifyAll();
}
}
}
}).start();
new Thread(new Runnable(){
public void run(){
while(true){
synchronized (p) {
if(!flags)
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
p.get();
flags =false;
p.notifyAll();
}
}
}
}).start();
}
}
public class ThreadDemo4 {
private static boolean flags =false;
public static void main(String[] args){
class Goods{
private String name;
private int num;
public synchronized void produce(String name){
if(flags)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name =name+"编号:"+num++;
System.out.println("生产了...."+this.name);
flags =true;
notifyAll();
}
public synchronized void consume(){
if(!flags)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("消费了******"+name);
flags =false;
notifyAll();
}
}
final Goods g =new Goods();
new Thread(new Runnable(){
public void run(){
while(true){
g.produce("商品");
}
}
}).start();
new Thread(new Runnable(){
public void run(){
while(true){
g.consume();
}
}
}).start();
}
}
public class ThreadDemo4 {
private static boolean flags =false;
public static void main(String[] args){
class Goods{
private String name;
private int num;
public synchronized void produce(String name){
while(flags)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name =name+"编号:"+num++;
System.out.println(Thread.currentThread().getName()+"生产了...."+this.name);
flags =true;
notifyAll();
}
public synchronized void consume(){
while(!flags)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"消费了******"+name);
flags =false;
notifyAll();
}
}
final Goods g =new Goods();
new Thread(new Runnable(){
public void run(){
while(true){
g.produce("商品");
}
}
},"生产者一号").start();
new Thread(new Runnable(){
public void run(){
while(true){
g.produce("商品");
}
}
},"生产者二号").start();
new Thread(new Runnable(){
public void run(){
while(true){
g.consume();
}
}
},"消费者一号").start();
new Thread(new Runnable(){
public void run(){
while(true){
g.consume();
}
}
},"消费者二号").start();
}
}
/*
消费者二号消费了******商品编号:48049
生产者一号生产了....商品编号:48050
消费者一号消费了******商品编号:48050
生产者一号生产了....商品编号:48051
消费者二号消费了******商品编号:48051
生产者二号生产了....商品编号:48052
消费者二号消费了******商品编号:48052
生产者一号生产了....商品编号:48053
消费者一号消费了******商品编号:48053
生产者一号生产了....商品编号:48054
消费者二号消费了******商品编号:48054
生产者二号生产了....商品编号:48055
消费者二号消费了******商品编号:48055
*/
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有