论坛首页 招聘求职论坛

遇到上机考试了,悲催的。

浏览 15509 次
精华帖 (0) :: 良好帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-14  
hngmduyi 写道
javaOak 写道

来个简洁点的:

/**
 * 
 * 写两个线程,对一个全局变量i,线程A对其从1加到1000,线程B 在i加1后对其显示. 
 *
 */
public class SynchronousQueueTest {
	private SynchronousQueue<Integer> sq = new SynchronousQueue<Integer>();
	private AtomicInteger ai = new AtomicInteger();
	
	class AddThread implements Runnable{
		public void run(){
			for(int i=0;i<1000;i++){
				try {
					sq.put(ai.incrementAndGet());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class ShowThread implements Runnable{
		public void run() {
			while(true){
				try {
					System.out.println(sq.take());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		ExecutorService es = Executors.newFixedThreadPool(2);
		SynchronousQueueTest sqt = new SynchronousQueueTest();
		AddThread at = sqt.new AddThread();
		ShowThread st = sqt.new ShowThread();
		es.submit(at);
		es.submit(st);
	}
}
 

这个好像也没有实现线程A加一后,等待,然后线程B输出i的值,然后线程A再执行....

运行下不就知道了,典型的生产者-消费者问题

0 请登录后投票
   发表时间:2012-03-15  
这是楼主面试的第一题目的答案
0 请登录后投票
   发表时间:2012-03-15  
这是楼主面试的第一次答案


public class Test {
		
	int i;
	public void Aplus(){
		new Thread(new A()).start();
	}
  
	class  A  implements Runnable{
 
		public  synchronized void run() {
			for(int  j = 0 ; j< 1001; j++){
			  i++;
			  try {
				  new Thread(new B()).start();
				  this.wait();
			  			} catch (InterruptedException e) {
				e.printStackTrace();
				}

		 	}
		}
	}
	class B implements Runnable{
			
		public synchronized void run() {
			
			System.out.println(i + 1);
				while(i == 999){
						new Thread(new A()).stop();
						}
							this.notify(); 
							new Thread(new A()).start();	 
					}
		
				}

	public static void main(String[] args){
		 new Test().Aplus();
		 
	}
}

 
  

0 请登录后投票
   发表时间:2012-03-16  

楼主,这边找到有一段代码可以通过递归的方式把一列任意数字进行排列组合的代码,楼主可以进行下载运行,

但是无法正确的对其包括重复数字时进行排列组合,并且无法解决第六题中4不可以在第三位,与3与5不可以

在一起的这两个条件,我在这一代码的方面计划通过增加与修改使其符合这两个条件,但是经过2小时++的尝试,没有找到具体方法

代码贴上

import java.util.Scanner; 

public class Permute { 

    static int count = 0; 
    public static void main(String[] args) { 
        System.out.println("please input a String:"); 
        Scanner in = new Scanner(System.in); 
        String str = in.nextLine(); 
        Permute p = new Permute(); 
        System.out.println(str+"出现的所有排列如下:"); 
        p.permute(str); 
        System.out.println(); 
        System.out.println("总共有"+count+"种排列"); 
    } 

    private void permute(String str) { 
        this.permute(str.toCharArray(), 0, str.length() - 1); 
    } 
//从最后一个位置向前,依次对每个位置上可能出现的字符进行确定(如字符数组是{a,b,c,d},那么最后一个位置可能出现4种情况,确定好第四位置上的字符后,第三个位置可能出现三种情况依次类推) 
    private void permute(char[] charArray, int low, int high) { 
        int i; 
        if (high == low) {//如果是到了第一个位置(low是第一个位置的索引),或者只有一个字符,那么应该输出此字符串 
            String str = ""; 
            for (i = 0; i < charArray.length; i++) { 
                str += charArray[i]; 
            } 
            System.out.print(str+"  "); 
            count++; 
        } else { 
            for (int j = low; j <= high; j++) {//将某个位置上可能出现的字符进行遍历(如最后一个位置可能出现high+1种情况) 
                for (i = low; i < high; i++) {//将第low位置上的字符移到第high位置上 
                    char temp = charArray[i]; 
                    charArray[i] = charArray[i + 1]; 
                    charArray[i + 1] = temp; 
                } 
                permute(charArray, low, high - 1);//当第high位置上的字符确定后,就应该确定第high-1位置上的字符。 
            } 
        } 
    } 
} 


 

0 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics