第七章作业

第一题:每个线程分别与CPU交互,由CPU分配工作。
第二题:直接调用还是单线程,他是用主线程直接执行的。
第三题:创建状态:就是new一个线程
就绪状态:准备获得CPU的同意
阻塞状态:等待其他线程运行中
运行状态:正在执行CPU分配的任务中
死亡状态:线程自然执行完毕,外部干涉终止线程
4.代码:

public class xiancheng4 extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
Thread.currentThread().setName(“线程1”);
for (int i = 0; i <+ 5; i++) {
System.out.println(i+"—"+Thread.currentThread().getName());
}
}

}

public class xiancheng4_2 implements Runnable {

@Override
public void run() {
	Thread.currentThread().setName("线程2");
	// TODO Auto-generated method stub
	for (int i = 0; i <+5; i++) {
		System.out.println(i+"---"+Thread.currentThread().getName());
	}

}

public static void main(String[] args) {
	xiancheng4 x=new xiancheng4();
	xiancheng4_2 x2=new xiancheng4_2();
	Thread t1=new Thread(x2);
	// TODO Auto-generated method stub
	x.start();
	t1.start();

}

}

第五题:

public class quqian implements Runnable {
private int money=500;

@Override
public void run() {
	
		
	while(true){
		synchronized (this) {System.out.println(Thread.currentThread().getName()+"准备取钱"+"取钱完成");
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		money=money-100;
		if (money<=0) {
			System.out.println(Thread.currentThread().getName()+"取钱失败,余额不足");

// System.exit(0);
break;
}
}}

}
public static void main(String[] args) {
	quqian q1=new quqian();
	Thread t1=new Thread(q1,"张三");
	Thread t2=new Thread(q1,"张三的妻子");
	t1.start();
	t2.start();
	
}

}

学员操作:

public class xiancheng extends Thread {

@Override
public void run() {
	for (int i = 0; i <5; i++) {
		System.out.println(Thread.currentThread().getName()+"asd"+i);
	}
}

}

import java.util.Scanner;

public class test22 {

public static void main(String[] args)   {
	// TODO Auto-generated method stub

// test2 t=new test2();
// Scanner a=new Scanner(System.in);
// System.out.println(“请输入1-100的数字”);
// try {
// int b=a.nextInt();
// t.setAge(b);
// } catch (Exception e) {
// // TODO: handle exception
// e.printStackTrace();
//
// }

// xiancheng x=new xiancheng();
// x.start();
jiekou j=new jiekou();
Thread a1=new Thread(j);
Thread a2=new Thread(j);
a1.start();
try {
a1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

	a2.start();
	
	

}

}

public class jielisaipao implements Runnable {
private int num1=1000;
private int num2=10;
public void run() {
while (true) {
synchronized (this) {
if (num1==0) {
System.out.println(“asdsad”);
break;
}else {

		System.out.println(Thread.currentThread().getName()+"号选手拿到了接力棒");
		for (int i = 1; i <=10; i++) {
			
			System.out.println(Thread.currentThread().getName()+"号跑了"+(i*num2)+"米");

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}num1=num1-100;
	System.out.println(num1);
		}
	}
}

}

import java.util.HashMap;
import java.util.Hashtable;

public class jieli {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	jielisaipao j=new jielisaipao();
	Thread t1=new Thread(j,"1");
	Thread t2=new Thread(j,"2");
	Thread t3=new Thread(j,"3");
	Thread t4=new Thread(j,"4");
	Thread t5=new Thread(j,"5");

// t1.start();
t2.start();
t3.start();
t4.start();
t5.start();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值