1至100,一个线程打印奇数,一个线程打印偶数

小弟夏天面试某家游戏公司,面试官出了这么一个问题

从1到100,一个线程打印奇数,一个线程打印偶数,按顺序打印出来。

自己如今写了代码,分享大家:

public class A {
	private int i;

	public A() {
		// TODO Auto-generated constructor stub
		i = 1;
	}

	synchronized public void printji() {
		if (i % 2 == 1) {
			System.out.println("Thread id:"+Thread.currentThread().getId()+"--->"+i);
			i++;
		}
	}

	synchronized public void printou() {
		if (i % 2 == 0) {
			System.out.println("Thread id:"+Thread.currentThread().getId()+"--->"+i);
			i++;
		}
	}

	public int geti() {
		return i;
	}
}

public class Thread1 extends Thread {
	private A a;

	public Thread1(A a) {
		// TODO Auto-generated constructor stub
		this.a = a;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while (a.geti() < 100)
			a.printji();
	}
}


public class Thread2 extends Thread {
	private A a;

	public Thread2(A a) {
		// TODO Auto-generated constructor stub
		this.a = a;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		while (a.geti() < 100)
			a.printou();
	}
}



public class Main {
	public static void main(String[] args) {
		A a=new A();
		Thread1 thread1=new Thread1(a);
		Thread2 thread2=new Thread2(a);
		thread1.start();
		thread2.start();
	}

}



//如有不对,还望大家指出。


打印结果:

Thread id:9--->1
Thread id:10--->2
Thread id:9--->3
Thread id:10--->4
Thread id:9--->5
Thread id:10--->6
Thread id:9--->7
Thread id:10--->8
Thread id:9--->9
Thread id:10--->10
Thread id:9--->11
Thread id:10--->12
Thread id:9--->13
Thread id:10--->14
Thread id:9--->15
Thread id:10--->16
Thread id:9--->17
Thread id:10--->18
Thread id:9--->19
Thread id:10--->20
Thread id:9--->21
Thread id:10--->22
Thread id:9--->23
Thread id:10--->24
Thread id:9--->25
Thread id:10--->26
Thread id:9--->27
Thread id:10--->28
Thread id:9--->29
Thread id:10--->30
Thread id:9--->31
Thread id:10--->32
Thread id:9--->33
Thread id:10--->34
Thread id:9--->35
Thread id:10--->36
Thread id:9--->37
Thread id:10--->38
Thread id:9--->39
Thread id:10--->40
Thread id:9--->41
Thread id:10--->42
Thread id:9--->43
Thread id:10--->44
Thread id:9--->45
Thread id:10--->46
Thread id:9--->47
Thread id:10--->48
Thread id:9--->49
Thread id:10--->50
Thread id:9--->51
Thread id:10--->52
Thread id:9--->53
Thread id:10--->54
Thread id:9--->55
Thread id:10--->56
Thread id:9--->57
Thread id:10--->58
Thread id:9--->59
Thread id:10--->60
Thread id:9--->61
Thread id:10--->62
Thread id:9--->63
Thread id:10--->64
Thread id:9--->65
Thread id:10--->66
Thread id:9--->67
Thread id:10--->68
Thread id:9--->69
Thread id:10--->70
Thread id:9--->71
Thread id:10--->72
Thread id:9--->73
Thread id:10--->74
Thread id:9--->75
Thread id:10--->76
Thread id:9--->77
Thread id:10--->78
Thread id:9--->79
Thread id:10--->80
Thread id:9--->81
Thread id:10--->82
Thread id:9--->83
Thread id:10--->84
Thread id:9--->85
Thread id:10--->86
Thread id:9--->87
Thread id:10--->88
Thread id:9--->89
Thread id:10--->90
Thread id:9--->91
Thread id:10--->92
Thread id:9--->93
Thread id:10--->94
Thread id:9--->95
Thread id:10--->96
Thread id:9--->97
Thread id:10--->98
Thread id:9--->99
Thread id:10--->100













你可能感兴趣的:(1至100,一个线程打印奇数,一个线程打印偶数)