论坛首页 编程语言技术论坛

答复: 迅雷亲历面经:JAVA 笔试+上机+面试(完整面试题大讨论)

浏览 21605 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-06   最后修改:2009-12-06
package com.test;

public class Test {
	public static void main(String[] args) {
		ThreadRun A = new ThreadRun();
		ThreadRun B = new ThreadRun();
		ThreadRun C = new ThreadRun();
		A.setName("A");
		A.setCanRun(true);
		A.setNextRun(B);
		B.setName("B");
		B.setNextRun(C);
		C.setName("C");
		C.setNextRun(A);
		A.start();
		B.start();
		C.start();
	}
}
class ThreadRun extends Thread
{
	//private static int runTime = 1;
	private ThreadRun nextRun;
	
	
	private boolean canRun;
	
	
	
	public ThreadRun getNextRun() {
		return nextRun;
	}



	public void setNextRun(ThreadRun nextRun) {
		this.nextRun = nextRun;
	}



	


	public boolean isCanRun() {
		return canRun;
	}



	public void setCanRun(boolean canRun) {
		this.canRun = canRun;
	}



	public void run() {
		for (int i = 0; i < 10; ) {
			if(this.canRun)
			{
				System.out.println(this.getName());
				nextRun.setCanRun(true);
				this.setCanRun(false);
				i++;
				
			} else
				try {
					this.sleep(1);
				} catch (InterruptedException e) {
					
					e.printStackTrace();
				}
		}
		
		
	}
}

0 请登录后投票
   发表时间:2009-12-07   最后修改:2009-12-08
来个好玩的
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class TBThread {
	public static void main(String[] args) {
		List<String>  work = new ArrayList<String>(Arrays.asList("123456789".split("")));
		work.removeAll(Collections.singletonList(""));
		final List<String> task = new ArrayList<String>(work);
		Collections.shuffle(work);
		System.out.print("work:");
		for(String s :work)System.out.print(","+s);
		System.out.print("\ntask:");
		for(String s :task)System.out.print(","+s);
		System.out.print("\nexec:");
		
		for(final String s :work){
			new Thread(){
				public void run() {
					try {
						sleep(new Random().nextInt(5000));
						synchronized (task) {
							if(task.isEmpty()) return;
							while(!s.equals(task.get(0))) task.wait();
							System.out.print(","+task.remove(0));
							task.notifyAll();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
	}
}
0 请登录后投票
   发表时间:2009-12-07  
楼主,这可是人家的机密。。。你。。。你。。。你。。。
0 请登录后投票
   发表时间:2009-12-07  
java中用Executors.newSingleThreadExecutor(),执行线程行不。
0 请登录后投票
   发表时间:2009-12-09  
好像是深圳的面试题,
0 请登录后投票
   发表时间:2009-12-10   最后修改:2009-12-10

interface IWakeAble{
    void wakeMe();
    void addSleepListener(ISleepListener listener);
}
interface ISleepListener{
    void beforeSleep(Object sender);
}
ThreadA,b,c 实现 IWakeAble;然后用一个ThreadManager去监听beforeSleep事件,一个完成了就wake下一个。


0 请登录后投票
   发表时间:2009-12-11  
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class TestThread {

/**
* @param args
*/
public static void main(String[] args) {
MyThread b = null;
MyThread c = null;
MyThread a = null;
Status status = new Status();
a = new MyThread("A", 0, status);
b = new MyThread("B", 1, status);
c = new MyThread("C", 2, status);
b.start();
c.start();
a.start();
}
}

class Status {
private int all = 30;

private int i = 0;
private boolean isOver=false;

public boolean isOver() {
return isOver ;
}

public synchronized void next() {
if(all-1>i){
i++;
}else{
isOver=true;
}
}
public int getStatus(){
return i%3;
}
}

class MyThread extends Thread {
public int status;
public Status s;
public MyThread(String name, int status, Status s) {
super(name);
this.status = status;
this.s = s;
}

public void run() {
while (!s.isOver()) {
if (s.getStatus()== status) {
System.out.print(this.getName());
s.next();
}
}
}
}
0 请登录后投票
   发表时间:2009-12-11  
闲来也写个,我这不用同步实现..
public class TestThread {
    static int num = 1;

    public static void main(String ds[]){
        new DemoThread(1,"A").start();
        new DemoThread(2,"B").start();
        new DemoThread(0,"C").start();
    }

    static class DemoThread extends Thread {
        int id;
        String pintln_str;
       
        public DemoThread(int id,String pintln_str) {
            this.id = id;
            this.pintln_str = pintln_str;
        }

        int print_num = 10;

        public void run() {           
            while (print_num > 0) {
                if (num%3 == id) {
                    System.out.print(pintln_str);
                    num ++ ;
                    print_num--;
                }
            }
        }
    }
}
0 请登录后投票
   发表时间:2009-12-11  
java synchronized方法就搞定了 有那么麻烦吗?
0 请登录后投票
   发表时间:2009-12-12  
线程方面不太熟悉,正在学习中,借鉴楼上的了各种方法了
0 请登录后投票
论坛首页 编程语言技术版

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