最近去迅雷面的试,里面很多是文件流和线程,呵呵,基本上我是做web的,所以也没什么太大兴趣,就是去看看,最后一题,整理了一下,大家有兴趣就看看吧
3个线程,要求打出来顺序一定要是“ABCABC“
public class Test {
public static void Main(String[] s){
Status status=new Status();
MyThread t1=new MyThread("A",status);
MyThread t2=new MyThread("B",status);
MyThread t3=new MyThread("C",status); t1.start();
t2.start();
t3.start();
}
class MyThread extends Thread(){
String id;
Status status;
int myStatus;
MyThread(String id,Status status){
this.id=id;
this.status=status;
myStatus=id-"A";
}
public void run(){
for(int i=0;i<10;i++){
Synchronized (填空1){
while(填空2){
try{
填空3;
}
catch(InterruptedException e){
e.printStack();
}
System.out.print(id);
Status.setStatus(填空4);
填空5;
}
}
}
}
}
class Status {
public int status=0;
public int getStatus(){
return status%3;
}
public void setStatus(int status){
this.status=status;
}
3个线程,要求打出来顺序一定要是“ABCABC“
public class Test {
public static void Main(String[] s){
Status status=new Status();
MyThread t1=new MyThread("A",status);
MyThread t2=new MyThread("B",status);
MyThread t3=new MyThread("C",status); t1.start();
t2.start();
t3.start();
}
class MyThread extends Thread(){
String id;
Status status;
int myStatus;
MyThread(String id,Status status){
this.id=id;
this.status=status;
myStatus=id-"A";
}
public void run(){
for(int i=0;i<10;i++){
Synchronized (填空1){
while(填空2){
try{
填空3;
}
catch(InterruptedException e){
e.printStack();
}
System.out.print(id);
Status.setStatus(填空4);
填空5;
}
}
}
}
}
class Status {
public int status=0;
public int getStatus(){
return status%3;
}
public void setStatus(int status){
this.status=status;
}
有个地方写错了, myStatus=id-"A";
我等你改好我来看。
while(填空2){
try{
填空3;
}catch(InterruptedException e){
e.printStack();
}
}
System.out.print(id);
Status.setStatus(填空4);
填空5; 如果是这样,可这样写:
1: status
2: status.getStatus() != myStatus
3: status.wait()
4: (myStatus+1) % 3
5: status.notifyAll()另:问题同 4 楼
编译器是通不过的。
package testAll;public class Test2 {
public static void main(String[] s){
Status status=new Status();
MyThread t1=new MyThread("A",status);
MyThread t2=new MyThread("B",status);
MyThread t3=new MyThread("C",status);
t1.start();
t2.start();
t3.start();
}
}class MyThread extends Thread{
/** 要输出的 A B C */
String id;
/** 维护共享资源-->状态, 初始时为 0 */
Status status;
/**
* 自己当前的状态
* 根据初始化可知
* 传 A 的时候自己为 0
* 传 B 的时候自己为 1
* 传 C 的时候自己为 2
*/
int myStatus;
MyThread(String id,Status status){
this.id=id;
this.status=status;
myStatus=id.toCharArray()[0]-'A';
}
public void run(){
for(int i=0; i<10; i++){
// 对共享资源加锁, 保证某一个线程拿到这个资源的锁时, 其它线程则等待
synchronized (status){//填空1: // 假设(线程1)进入
// 1. 如果共享资源的状态不等于自己(线程1)的状态, (线程1)进入等待状态, 同时释放掉自己的锁, 保证其它线程能进入.
// 此时假设(线程2)进入
// 2. 如果共享资源的状态等于它自己(线程2)的状态, 就跳过while, 输出 id, 并将修改共享资源的状态,
// 再for循环过来的时候, 发现共享资源的状态与自己(线程2)的状态不同了, 则自己进入等待状态, 同时释放掉自己的锁
// 依此循环
while(status.getStatus()!=myStatus){//填空2:
try{
// 释放自己有的对象锁, 并唤醒其它等待的线程进入, 其它线程随机被选中一个执行.
status.wait();//填空3;
}
catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.print(id);
// 修改共享资源的状态,保证当前线程不会连续输出
status.setStatus(myStatus+1);//填空4
// 自己退出时, 唤醒其它线程
status.notifyAll();//填空5
}
}
}
}class Status {
public int status=0;
public int getStatus(){
return status%3;
}
public void setStatus(int status){
this.status=status;
}
}
public static void main(String[] s){
Status status=new Status();
MyThread t1=new MyThread("A",status);
MyThread t2=new MyThread("B",status);
MyThread t3=new MyThread("C",status); t1.start();
t2.start();
t3.start();
}
}
class MyThread extends Thread{
String id;
Status status;
int myStatus;
MyThread(String id,Status status){
this.id=id;
this.status=status;
myStatus=id.charAt(0)-'A';
} public void run(){
for(int i=0;i <10;i++){
synchronized (/*填空1*/status){ while(/*填空2*/status.getStatus() != myStatus ){
try{
//填空3;
status.wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.print(id);
status.setStatus(/*填空4*/myStatus+1);
// 填空5;
status.notifyAll(); }
}
}
}
class Status {
public int status=0;
public int getStatus(){
return status%3;
}
public void setStatus(int status){
this.status=status;
}
}6楼dengsf绝对是牛人,填空3我本来想的是sleep,但是这样填空5就没法填了。
呵呵,参考了一下你的答案,也算是抄袭啦!