`

多线程实现打印ABBABBABBABB....

    博客分类:
  • JAVA
阅读更多
class TestThread implements Runnable {

	int y = 1;
	public static void main(String args[]) throws InterruptedException {
    TestThread t = new TestThread();
		Thread t1 = new Thread(t);
		t1.start();
		Thread t2 = new Thread(t);
		t2.start();
	}

	char x = 'A';
	String s = new String();

	public void run() {
		
		while (y < 60) { 
			synchronized (s) {
				if (y%3==1) {
					x = 'A'; 
					System.out.print(x);
					
				} else {
					x = 'B';
					System.out.print(x);
					
				}
				y++;
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


	public static int getMiddle(int[] list, int low, int high) {
		
		int tmp = list[low];
		while(low <high) {
			
			while(low < high && list[high] > tmp) {
				high--;
			}
			
			list[low] = list[high];
			
			while(low < high && list[low] < tmp) {
				
				low++;
			}
			list[high] = list[low];
		}
		list[low] = tmp;
		return low;	
	}
	
	public static void quickSort(int[] list, int low, int high) {
		for(int i=0;i< list.length ; i++) {
	    	System.out.print(list[i] + ",");
	    }
		System.out.println();
		int mid = getMiddle(list,low,high);
		quickSort(list,low,mid-1);
		quickSort(list,mid+1,high);
	}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics