当前位置:编程学习 > JAVA >>

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

转另一个论坛的帖子:

迅雷面试回来,用了整整一下午(不知道怎么说了,其中等待时间都快2小时了),自己感觉笔试和上机还可以,但技术面谈这一关答得不太好,现在再次感觉互联网公司与一般软件公司的区别了,其中一点就是互联网应用在性能上要求很高,谈了一个小时大部分题目感觉都在谈论性能问题,自己在方面一直是弱项,汗啊:( 

仔细回忆了一下整个面试过程的题目,记录下来,希望大家多多给点意见讨论下啊 

一、笔试题: 
A)JAVA基础多项选择题,比较简单,略 

B)问答: 
1)ajax原理、如何实现刷新数据及优点? 
2)门面模式的解释、适用场合? 
3)写6个linux常用命令? 
4)SQL语句题,较简单 

C)编程: 
1)有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC… 
2)假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)? 

二、上机题: 
Java上机实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数? 

三、面试题: 
1、说说JVM原理?内存泄露与溢出区别,何时产生内存泄露? 
2、用java怎么实现有每天有1亿条记录的DB存储?mysql上亿记录数据量的数据库如何设计? 
3、mysql支持事务吗?DB存储引擎有哪些? 
4、mvc原理,mvc模式的优缺点,如果让你设计你会怎么改造MVC? 
5、hibernate支持集群吗?如何实现集群? 
6、tomcat 最多支持并发多少用户? 
7、map原理,它是如何快速查找key的?map与set区别? 
8、描术算法,如何有效合并两个文件:一个是1亿条的用户基本信息,另一个是用户每天看电影连续剧等的记录,5000万条。内存只有1G??? 
9、在1亿条用户记录里,如何快速查询统计出看了5个电影以上的用户? 
10、Spring如何实现IOC与AOP的,说出实现原理? 

期待大家的探讨,共同提高,多谢  --------------------编程问答-------------------- --------------------编程问答-------------------- 这种公司真会搞噱头

那么难 他们自己公司又有几个人都会? --------------------编程问答-------------------- 此贴必火,坚定完毕 --------------------编程问答--------------------
引用楼主 wmbjhlj 的回复:
12)假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)?

对这个比较感兴趣 --------------------编程问答-------------------- --------------------编程问答-------------------- 看一遍下来,感觉又把我做好的很少~~~   - -! --------------------编程问答-------------------- 基本只懂java语言是不可能找个java的工作了。 --------------------编程问答-------------------- --------------------编程问答-------------------- 迅雷要的就是速度 --------------------编程问答--------------------
引用 7 楼 zangxt 的回复:
基本只懂java语言是不可能找个java的工作了。

有道理! --------------------编程问答-------------------- --------------------编程问答-------------------- 妈呀 做梦学习去了  --------------------编程问答-------------------- --------------------编程问答-------------------- 顶一下。。。 --------------------编程问答-------------------- 能完整的把题目做完,工资肯定不低于1.5万. --------------------编程问答-------------------- 恩 要求全才啊 呵呵

感觉对于应届生有点要求小高 --------------------编程问答--------------------
引用 16 楼 justinavril 的回复:
恩 要求全才啊 呵呵

感觉对于应届生有点要求小高

呵呵,估计这是问题总结吧。问一个人这么多,感觉可能性不大。 --------------------编程问答-------------------- 能答好这些题,得是工作几年的人呢? --------------------编程问答-------------------- 接分,
对这个问题十分感兴趣:
mvc原理,mvc模式的优缺点,如果让你设计你会怎么改造MVC? 

麻烦大牛们发表点意见 --------------------编程问答-------------------- very hard --------------------编程问答-------------------- 精通里面的任意一个就不简单了! --------------------编程问答-------------------- 考地也太杂了 --------------------编程问答-------------------- lz浪费啊,光面试题,就能开10个帖子了 --------------------编程问答-------------------- 好多都不明白啊,java web才起步,要学的很多啊,基本上面试题 觉得没经验的人去了,百分之九十的新手都答不上来 --------------------编程问答-------------------- --------------------编程问答-------------------- 太难了 --------------------编程问答-------------------- 好多啊 --------------------编程问答-------------------- 难!!! --------------------编程问答-------------------- --------------------编程问答-------------------- 难 --------------------编程问答-------------------- 看此贴之后
有又矮一节的感受 --------------------编程问答-------------------- 看来我们这些菜鸟的路还很长啊 --------------------编程问答-------------------- 一个字:难! --------------------编程问答-------------------- 受不了这样的面试! --------------------编程问答-------------------- --------------------编程问答-------------------- 确实有点难。

7、map原理,它是如何快速查找key的?map与set区别?
这里应该问hashMap的原理才对吧?单纯的map只是一个接口,各种不同的实现类采用的算法可能不一样。

8、描术算法,如何有效合并两个文件:一个是1亿条的用户基本信息,另一个是用户每天看电影连续剧等的记录,5000万条。内存只有1G???
这个有点意思,等大牛来讲。1亿条用户信息,那文件得多大了?

9、在1亿条用户记录里,如何快速查询统计出看了5个电影以上的用户?
建索引。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 同感 --------------------编程问答-------------------- 路过 --------------------编程问答-------------------- 只有一半是能吹的,而且还不敢在这里吹,惭愧中~~~~~~~~ --------------------编程问答-------------------- 希望牛人解答!!! --------------------编程问答-------------------- 刚毕业  这样的题  太受打击了   --------------------编程问答-------------------- 看来我是找不到工作了。 --------------------编程问答-------------------- 望题生畏啊 --------------------编程问答-------------------- 可以呀,问下他可以提供的待遇是多少 --------------------编程问答-------------------- --------------------编程问答-------------------- 路过~并学习高手解答

留帖备查! --------------------编程问答--------------------
引用 17 楼 zangxt 的回复:
引用 16 楼 justinavril 的回复:
 恩 要求全才啊 呵呵

 感觉对于应届生有点要求小高

 呵呵,估计这是问题总结吧。问一个人这么多,感觉可能性不大。

其实答的不全对 个别问题有亮点 我想也会很有机会的  --------------------编程问答-------------------- 二、上机题: 
Java上机实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数? 


/*文本内容仅限数字,字母,及空格*/
import java.io.*;
class Ex6_5 {
    public static void main ( String[] args ) {
        String fileName = "C:/Hello.txt" , line;
        int i,j,f,k;
        try {
             BufferedReader in = new BufferedReader(
                                              new FileReader( fileName  ) );
             line = in.readLine();   //读取一行内容
             while ( line != null ) { 
                 if(Character.isLetter(line))
                      i++;
                else if(Character.isDigit(line))
                      j++;
                 else
                      f++;
           line = in.readLine();
                   k++   
                 }
            in.close(); 
        System.out.println("字母"+i+"数字"+j+"空格"+j+"行数"+k)
        }
       catch ( IOException iox ) { 
            System.out.println("Problem reading " + fileName );  
        }
    }
}

对于这个算法题很感兴趣,期待高手作答
2)假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)?  --------------------编程问答-------------------- mark --------------------编程问答-------------------- 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

对这个感兴趣,等待高人 --------------------编程问答--------------------

import java.io.*;
class Ex6_5 {
    public static void main ( String[] args ) {
        String fileName = "C:/Hello.txt" , line;
        int i,j,f,k;
       
             BufferedReader in = new BufferedReader(
                                              new FileReader( fileName  ) );
             line = in.readLine();   //读取一行内容
               while ( line != null ) { 
                int cU = 0 ;
                int cL = 0 ;
                int cN = 0 ;
                char c ;
                for(int i=0 ;i<line.length() ;i++) {
                c = s.charAt(i) ;
                if(c > 'a' && c < 'z') {
                  cL ++ ;
                 }
                 else if(c > 'A' && c < 'Z') {
                  cU ++ ;
                 }
                 else {
                  cN ++ ;
                   }
         line = in.readLine();
                     k++ ; 
          }
         System.out.println("Number of LowerCase:" + cL) ;
         System.out.println("Number of UpperCase:" + cU) ;
         System.out.println("Number of NotChar:" + cN) ;
     }
 



写错了,在更正一下



--------------------编程问答-------------------- jf~~~~~~~~~~~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 关注中 --------------------编程问答--------------------
引用 52 楼 yukang_ky 的回复:
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

对这个感兴趣,等待高人


public class ThreadTest extends Thread {

public void run() {
for (int i = 0; i < 10; i++) {
System.out.print(this.getName());
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws InterruptedException {
ThreadTest a = new ThreadTest();
a.setName("A");
ThreadTest b = new ThreadTest();
b.setName("B");
ThreadTest c = new ThreadTest();
c.setName("C");
a.start();
b.start();
c.start();
}
}
--------------------编程问答-------------------- 8、描术算法,如何有效合并两个文件:一个是1亿条的用户基本信息,另一个是用户每天看电影连续剧等的记录,5000万条。内存只有1G??? 

核心算法就是归并算法。算法基本如下描述:

操作系统块大小是B字节(这个块是有效块,是人为规定,一般是4的倍数较为高效)。
主存是1*1024*1024*1024字节,设为M
记录占用R字节。

填满内存须要M/B次,一个块用于临时交互,那么一次可以为M/B-1个快集合填充。

得出B/R等于多少?用这个值进行划分记录。大约出现N个有限集合。

最后使用归并算法对N个集合进行归并。使用两趟归并算法就是TB数据集。基本够用。

说简单,实现也要一段时间,参考数据库大型算法中的多路归并就解决了。

9、在1亿条用户记录里,如何快速查询统计出看了5个电影以上的用户?

这个问题意义不大:

没有关系结构,电影重复看算不算5个?但是关系中count和用户信息都在一个临时记录中这倒是真的。 --------------------编程问答-------------------- 顶一下 --------------------编程问答-------------------- 呵呵,我关心的是他们能给出多高的薪水 --------------------编程问答--------------------
引用 56 楼 whut0802 的回复:
引用 52 楼 yukang_ky 的回复:
 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

 对这个感兴趣,等待高人

Java codepublicclass ThreadTestextends Thread {publicvoid run() {for (int i=0; i<10; i++) {
            System.out.print(this.getName());try {
                Thread.currentThread().sleep(2000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }publicstaticvoid main(String[] args)throws InterruptedException {
        ThreadTest a=new ThreadTest();
        a.setName("A");
        ThreadTest b=new ThreadTest();
        b.setName("B");
        ThreadTest c=new ThreadTest();
        c.setName("C");
        a.start();
        b.start();
        c.start();
    }
}

你要是这么写还不如不写,因为你是对线程的极度不熟悉,要是让你打印100次,你自己看看结果吧,cpu的时间片不是你想的那么简单的,这个题目得用同步来做,否则就不会是出现以上的结果(使用一个static类型的变量来控制打印的除外,因为考官压根就不是要考的这个) --------------------编程问答--------------------
引用楼主 wmbjhlj 的回复:
 2)假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)?

我只给个思路,东西得自己去摸索着写,建议看看,java.util.regex下面的东西(这下面貌似就3个类,其中还包括一个Exception),这个做起来比String的操作要快很多,尤其是用到正则表达式的时候。 --------------------编程问答-------------------- 多线程的题,我感觉这样可以,100次没有问题

public class testThread implements Runnable {

public void run() {
for(int i=0; i<1000; i++ ) {
System.out.print(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws InterruptedException {
testThread a = new testThread();
testThread b = new testThread();
testThread c = new testThread();

Thread ta = new Thread(a);
ta.setName("A");
Thread tb = new Thread(c);
tb.setName("B");
Thread tc = new Thread(b);
tc.setName("C");

ta.start();
Thread.sleep(50);
tb.start();
Thread.sleep(50);
tc.start();
}

} --------------------编程问答--------------------
引用 62 楼 raul317 的回复:
多线程的题,我感觉这样可以,100次没有问题

 public class testThread implements Runnable {

  public void run() {
  for(int i=0; i <1000; i++ ) {
  System.out.print(Thread.currentThread().getName());
  try {
  Thread.sleep(1000);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  }
  }

  public static void main(String[] args) throws InterruptedException {
  testThread a = new testThread();
  testThread b = new testThread();
  testThread c = new testThread();

  Thread ta = new Thread(a);
  ta.setName("A");
  Thread tb = new Thread(c);
  tb.setName("B");
  Thread tc = new Thread(b);
  tc.setName("C");

  ta.start();
  Thread.sleep(50);
  tb.start();
  Thread.sleep(50);
  tc.start();
  }

 }

i改成<10啊,后来没改 --------------------编程问答-------------------- 学习了 哈哈 --------------------编程问答-------------------- 我是来拿大鸭蛋的..... --------------------编程问答-------------------- 对于迅雷这样的要求也很正常,题目不算太难 --------------------编程问答-------------------- 自卑中…… --------------------编程问答-------------------- 哎呀!mark --------------------编程问答--------------------
引用 7 楼 zangxt 的回复:
基本只懂java语言是不可能找个java的工作了。
说得太正确了,深有感受! --------------------编程问答-------------------- 难 --------------------编程问答--------------------
char []charNum={'6','s','a','b','c','s','s','s','f','s','f','s','3'};

for(int i=0;i<charNum.length;i++)
{
if('a'==charNum[i] || 'b'==charNum[i] || '3'==charNum[i])
{
char []temp=new char[charNum.length-2];
for(int j=0;j<i;j++)
{
temp[j]=charNum[j];
}
for(int j=i+1;j<charNum.length;j++)
{
if(j!=charNum.length-1)
{
temp[j-1]=charNum[j];
}

}
charNum=temp;
i--;
continue;

}
}
--------------------编程问答-------------------- 好多不清楚,前方的路还远着,先mark一下! --------------------编程问答-------------------- 好 --------------------编程问答-------------------- 不错,顶。。。。。。。。。 --------------------编程问答-------------------- 学了一年java,差不多全部做不出来!我怕我得睡觉的梦中都在自动看书才行! --------------------编程问答-------------------- 看看再看看  越来越想学啦   为什么啦 》》》???有挑战性 --------------------编程问答-------------------- 我唯一能想到的是替换字符串那个题。。。用正则表达式 --------------------编程问答--------------------
引用 71 楼 wzju64676266 的回复:
Java codechar []charNum={'6','s','a','b','c','s','s','s','f','s','f','s','3'};for(int i=0;i<charNum.length;i++)
        {if('a'==charNum[i]||'b'==charNum[i]||'3'==charNum[i])
            {char []temp=?-


这面试题有这么简单就好了。。。
--------------------编程问答-------------------- 惭愧中,唉……感觉好难啊,真的要很厉害才行啊 --------------------编程问答-------------------- 很有代表性的题目  不过



惭愧    惭愧   




   面壁思过去.... --------------------编程问答--------------------
引用 60 楼 dlnu05610 的回复:
引用 56 楼 whut0802 的回复:
引用 52 楼 yukang_ky 的回复:
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

对这个感兴趣,等待高人

Java codepublicclass ThreadTestextends Thread {publicvoid run() {for (int i=0; i <10; i++) {
            System.out.print(this.getName());try {
                Thread.currentThread().sleep(2000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }publicstaticvoid main(String[] args)throws InterruptedException {
        ThreadTest a=new ThreadTest();
        a.setName("A");
        ThreadTest b=new ThreadTest();
        b.setName("B");
        ThreadTest c=new ThreadTest();
        c.setName("C");
        a.start();
        b.start();
        c.start();
    }
}

你要是这么写还不如不写,因为你是对线程的极度不熟悉,要是让你打印100次,你自己看看结果吧,cpu的时间片不是你想的那么简单的,这个题目得用同步来做,否则就不会是出现以上的结果(使用一个static类型的变量来控制打印的除外,因为考官压根就不是要考的这个)


我感觉这这里哪怕是执行100次,1000次都可以的.CPU的时间片肯定比执行3条打印语句所花的时间要大,那么在每个for循环里面的动作都不可能被时间片的切换都打断,故而每次睡眠后都能够及时的按照优先级打印,顺序不会紊乱,我试过1000次都没有出错.但是要注意一点就是睡眠时间一定要大于3次执行的打印语句.
我试过把睡眠时间控制在500以上不管多少次打印顺序都不会出错,但是如果时间在500以下就很容易出现打印顺序不稳定的现象,这个时候就如楼上所说的出现不稳定的现象。更多的关于多线程的理论比较复杂期待高手深入讨论,祝楼主好运. --------------------编程问答-------------------- 难!!! --------------------编程问答-------------------- MARK,期待高人出现 --------------------编程问答-------------------- - -~! --------------------编程问答-------------------- 来自是一位csdn朋友的贴,转载,呵呵,大家共同讨论,学习


package com.saturday;

public class KMP {
    public static String myReplaceAll(
            String input,
            String replaced){
        
        //短路掉不可能匹配的情况
        if(replaced.equals("")
         ||input.length()<replaced.length()){
            return input;
        }
        
        StringBuffer buf=new StringBuffer();
        char[] s=input.toCharArray();
        char[] r=replaced.toCharArray();             
        
        int rLen=r.length;
        int matchCount;    
        
        for(int i=0,sLen=s.length;i<sLen;){            
            if(i<sLen-rLen){
                //检查匹配
                matchCount=0;                
                for(int j=0;j<rLen;j++){
                    if(s[i+j]==r[j]){
                        matchCount++;                        
                    }else{
                        break;
                    }
                }        
                //完全匹配
                if(matchCount==rLen){
                    i+=rLen;
                    continue;
                }                
                //部分匹配
                if(matchCount>0){
                    for(int j=0;j<matchCount;j++){
                        buf.append(s[i++]);
                    }
                    continue;
                }
            }            
            
            buf.append(s[i++]);
        }        
        
        return buf.toString();
    }
    
    
    public static void main(String[] args){
        String input="123 mfc mf fc mfdC mfmfc cmfc dd abc";
        String replaced="1234";
        
        System.out.println(
            myReplaceAll(input,replaced).equals(input.replaceAll(replaced,""))
        );
    }
}



from
http://topic.csdn.net/u/20091130/23/79caaaa8-fb36-4dc4-a23a-13e816a28329.html?seed=653129929&r=61557319#r_61557319 --------------------编程问答-------------------- up --------------------编程问答--------------------
引用 85 楼 yang677888 的回复:
来自是一位csdn朋友的贴,转载,呵呵,大家共同讨论,学习

Java codepackage com.saturday;publicclass KMP {publicstatic String myReplaceAll(
            String input,
            String replaced){//短路掉不可能匹配的情况if(replaced.equals("")||input.length()<replaced.length()){return input;
        }
        
        StringBuffer buf=new StringBuffer();char[] s=input.toCharArray();char[] r=replaced.toCharArray();int rLen=r.length;int matchCount;for(int i=0,sLen=s.length;i<sLen;){if(i<sLen-rLen){//检查匹配                matchCount=0;for(int j=0;j<rLen;j++){if(s[i+j]==r[j]){
                        matchCount++;                        
                    }else{break;
                    }
                }//完全匹配if(matchCount==rLen){
                    i+=rLen;continue;
                }//部分匹配if(matchCount>0){for(int j=0;j<matchCount;j++){
                        buf.append(s[i++]);
                    }continue;
                }
            }            
            
            buf.append(s[i++]);
        }return buf.toString();
    }publicstaticvoid main(String[] args){
        String input="123 mfc mf fc mfdC mfmfc cmfc dd abc";
        String replaced="1234";
        
        System.out.println(
            myReplaceAll(input,replaced).equals(input.replaceAll(replaced,""))
        );
    }
}


from
http://topic.csdn.net/u/20091130/23/79caaaa8-fb36-4dc4-a23a-13e816a28329.html?seed=653129929&r=61557319#r_61557319


不是说不准用String的内置函数吗?要是这么做,有谁不会做? --------------------编程问答-------------------- 大公司能随便进,那么毕业就不失业了。 --------------------编程问答-------------------- 国内大型电信供应商,高薪诚聘JAVA,数据库开发人员

要求如下: 
1、计算机、软件、通讯等相关专业; 
2、本科2年以上JAVA开发经验, 硕士1年以上相关经验; 
3、能够掌握当前主流的J2EE开发框架中的一种,或者熟悉ORACLE等大型关系数据库; 
4、工作地:深圳。 
如有意愿,请把简历发送到:hwzhp2007@gmail.com. 
欢迎来信咨询,有信必复!
薪水上不封顶,只要能力强,开多少都可以满足!
职位:系统分析,架构设计,高级程序员,程序员,数据库开发人员,软件测试等。
--------------------编程问答-------------------- 迅雷不及掩耳盗铃之势 --------------------编程问答-------------------- 惭愧呀 ~~~  
--------------------编程问答-------------------- 没有能看懂的。。。 --------------------编程问答--------------------
引用 60 楼 dlnu05610 的回复:
引用 56 楼 whut0802 的回复:
引用 52 楼 yukang_ky 的回复:
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…

对这个感兴趣,等待高人

Java codepublicclass ThreadTestextends Thread {publicvoid run() {for (int i=0; i <10; i++) {
            System.out.print(this.getName());try {
                Thread.currentThread().sleep(2000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }publicstaticvoid main(String[] args)throws InterruptedException {
        ThreadTest a=new ThreadTest();
        a.setName("A");
        ThreadTest b=new ThreadTest();
        b.setName("B");
        ThreadTest c=new ThreadTest();
        c.setName("C");
        a.start();
        b.start();
        c.start();
    }
}

你要是这么写还不如不写,因为你是对线程的极度不熟悉,要是让你打印100次,你自己看看结果吧,cpu的时间片不是你想的那么简单的,这个题目得用同步来做,否则就不会是出现以上的结果(使用一个static类型的变量来控制打印的除外,因为考官压根就不是要考的这个)

不好意思,要是让我打印100次,结果还是循环的ABC...如果你的机子上没试验成功,你可以把睡眠时间加长一点。。。。至于你说的同步来做,愿听您的高见。 --------------------编程问答-------------------- --------------------编程问答-------------------- 一个都不会,我日 --------------------编程问答-------------------- 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC… 

public class TestABC implements Runnable{
  private int i=1;
     public synchronized void run()
{
while(i<=30)
{
if(i%3==0)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}

if(i%3==1)
{

System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}

else
{
System.out.print(Thread.currentThread().getName());
i++;
try{this.wait(100);}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.notify();
}

}

}
public static void main(String[] args) {

      TestABC t=new TestABC();
      Thread th=new Thread(t);
      th.setName("A");
      th.start();
      Thread th1=new Thread(t);
      th1.setName("B");
      th1.start();
      Thread th2=new Thread(t);
      th2.setName("C");
      th2.start();
      
     
}

}
public class TestABC implements Runnable{
  private int i=1;
     public synchronized void run()
{
while(i<=30)
{
if(i%3==0)
{
System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}

if(i%3==1)
{

System.out.print(Thread.currentThread().getName());
i++;
try {
this.wait(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notify();
}

else
{
System.out.print(Thread.currentThread().getName());
i++;
try{this.wait(100);}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.notify();
}

}

}
public static void main(String[] args) {

      TestABC t=new TestABC();
      Thread th=new Thread(t);
      th.setName("A");
      th.start();
      Thread th1=new Thread(t);
      th1.setName("B");
      th1.start();
      Thread th2=new Thread(t);
      th2.setName("C");
      th2.start();
      
     
}

}
结果是ABCABCABCABCABCABCABCABCABCABC  --------------------编程问答--------------------
package test;

public class Myreplace {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Myreplace.myReplaceAll("abcdefghhhhcedcdehabchhhh", "cde","!");
    }
    
    
    public static String myReplaceAll(String input,String regx,String rep)
    {
//        特殊情况判断

        char i [] = input.toCharArray();
        char r [] = regx.toCharArray();
        StringBuffer buf = new StringBuffer();
        boolean b = false;
        for(int k=0;k<i.length;k++)
        {
            if(i.length-r.length>0)
            {
                for(int j=0;j<r.length;j++)
                {
                    System.out.println(i[k+j]+"是否匹配"+r[j]);
                    if(i[k+j]==r[j])
                    {
                        b=true;
                    }
                    else
                    {
                        b=false;
                        break;
                    }
                }
            }
            if(b)
            {
                    k+=regx.length()-1;
                    buf.append(rep);
                
            }else
            {
                    buf.append(i[k]);
            }
        }
        System.out.println(input);
        System.out.println(buf);
        System.out.println(input.replace(regx, rep));
        
        
        return input;
    }

}

--------------------编程问答-------------------- ls这个靠谱 --------------------编程问答--------------------

/*
 *******************************************************************************
 * Operation.java
 *
 *<项目内容>
 *  项目名 : ThreeThreads
 *<类名>
 *  Operation
 *<功能概要>
 *  
 *<更新履历>
 *  日期        版本     作成者     描述
 *  2009-12-3    1.00    yanfengg  新规做成
 *******************************************************************************
 */
package com.icyy.test;

/**
 * <p>
 * [概 要] 
 * 描述一個操作的接口
 * [详 细] 
 * 
 * [备 考] 
 * 
 * [环 境] J2SE 1.5
 * </p>
 *
 * @author Sidney Yang
 * @version 1.0
 */
public interface Operation {

// 要執行的操作
public void run();
}



/*
 *******************************************************************************
 * Abc.java
 *
 *<项目内容>
 *  项目名 : ThreeThreads
 *<类名>
 *  Abc
 *<功能概要>
 *  
 *<更新履历>
 *  日期        版本     作成者     描述
 *  2009-12-3    1.00    Sidney Yang  新规做成
 *******************************************************************************
 */

package com.icyy.test;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * [概 要] 
 * 將一系列操作分配給不同的線程并重複同步依次執行這些線程(操作)直到滿足退出條件。
 * [详 细] 
 * 
 * [备 考] 
 * 
 * [环 境] J2SE 1.5
 * </p>
 *
 * @author Sidney Yang
 * @version 1.0
 */
public class Abc{

// 用於存放所有線程的list.
private List<ThreadsWithOperation> operArray = new ArrayList<ThreadsWithOperation>();
// 線程計數器,每有一個線程被執行,自增1.
private int threadCounter = 0;

// 添加一個帶操作的線程
public void push(Operation oper){
operArray.add(new ThreadsWithOperation(oper));
}

// 啟動所有線程
public void start(){
for(ThreadsWithOperation thread : operArray){
thread.start();
}
}

/**
 * 帶操作的線程類
 * @author Sidney Yang
 * @version 1.0
 */
class ThreadsWithOperation extends Thread{
// 表示一個操作的對象。
private Operation oper;

/**
 * 用一個Operation得對象構造一個ThreadsWithOperation
 * Constructors ThreadsWithOperation
 * @param oper 表示一個操作的對象
 */
ThreadsWithOperation(Operation oper){
this.oper = oper;
}


/**
 * 線程方法。執行流程為:
 *  1.判斷是否輪到本線程執行,若不是,則跳到3步驟。
 *  2.執行Operation對象里的操作,并將執行權移交給下一個線程。
 *  3.喚醒所有等待線程并判斷是否滿足退出條件,如果不滿足則跳到步驟5。
 *  4.將自己加入等待隊列。
 *  5.結束
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @see java.lang.Thread#run()
 * 
 */
@Override
public void run(){
while(!Abc.this.readyToQuit()){
synchronized(Abc.this){
if(isMyTurn()){
oper.run();
turnNext();
}
Abc.this.notifyAll();
if(!Abc.this.readyToQuit()){
try {
Abc.this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

/**
 * 
 * 判斷是否輪到本線程執行
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @return 如果輪到本線程執行返回true,否則返回false
 *
 */
private boolean isMyTurn(){
int threadIndex = Abc.this.operArray.indexOf(this);
return Abc.this.threadCounter % Abc.this.operArray.size() == threadIndex;
}

/**
 * 
 * 將執行權交給下一個線程
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 *
 */
protected void turnNext(){
Abc.this.threadCounter++;
}
}
/**
 * 
 * 此方法給出所有線程退出的條件,若返回true則所有線程結束,子類應根據情況重寫此方法。
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @return 若滿足線程結束條件返回true,否則返回false.
 *
 */
protected boolean readyToQuit() {
return false;
}

/**
 * 
 * 返回線程計數器
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @return 線程計數器的值
 *
 */
public int getThreadCounter(){
return threadCounter;
}

/**
 * 
 * 測試用main方法
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @param args 控制臺傳入的參數
 *
 */
public static void main(String[] args){
Abc abc = new Abc(){

/**
 * 重寫readyToQuit方法使3個線程各執行10次。
 *
 * @Create Date: 2009-12-3
 * @author Sidney Yang
 * @return 若滿足線程結束條件返回true,否則返回false.
 * @see com.icyy.test.Abc#readyToQuit()
 * 
 */
@Override
public boolean readyToQuit() {
return this.getThreadCounter() >= 30;
}
};

abc.push(new Operation(){
public void run() {
System.out.print("A");
}
});
abc.push(new Operation(){
public void run() {
System.out.print("B");
}
});
abc.push(new Operation(){
public void run() {
System.out.print("C");
}
});

abc.start();
}
}


粗略寫了一下,沒有完善,應該能滿足要求。 --------------------编程问答--------------------
引用 63 楼 raul317 的回复:
引用 62 楼 raul317 的回复:
 多线程的题,我感觉这样可以,100次没有问题

  public class testThread implements Runnable {

   public void run() {
   for(int i=0; i  <1000; i++ ) {
   System.out.print(Thread.currentThread().getName());
   try {
   Thread.sleep(1000);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   }
   }

   public static void main(String[] args) throws InterruptedException {
   testThread a = new testThread();
   testThread b = new testThread();
   testThread c = new testThread();

   Thread ta = new Thread(a);
   ta.setName("A");
   Thread tb = new Thread(c);
   tb.setName("B");
   Thread tc = new Thread(b);
   tc.setName("C");

   ta.start();
   Thread.sleep(50);
   tb.start();
   Thread.sleep(50);
   tc.start();
   }

  }

 i改成 <10啊,后来没改

按照你的写法,i<10后运行的结果,
ABCABCABCABCABCABCABCABCBACABC
多运行几遍,可能结果会不同,但是这个证明了一个问题就是可能会出现问题。。。。。
顺便BS下fanmaomao的为人,我写东西就是来探讨问题的,我说不对难道还有错了,难道我知道不对也不说?垃圾人品。。。。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,