java 中有没有类似stringutils.join用法.join

java 中有没有类似string.join_百度知道
java 中有没有类似string.join
提问者采纳
+ 或String.concat()如果多次连接,还是用StringBuffer比较好
来自团队:
其他类似问题
为您推荐:
string的相关知识
其他2条回答
直接用+好了。
mons.lang.StringUtils的函数实现
public static String join(Object[] array, char separator) {
if (array == null) {
int arraySize = array.
int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize);
StringBuffer buf = new StringBuffer(bufSize);
for (int i = 0; i & arrayS i++) {
if (i & 0) {
buf.append(separator);
if (array[i] != null) {
buf.append(array[i]);
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁java 中有没有类似string.join_百度知道
java 中有没有类似string.join
提问者采纳
你好:你就说你想干嘛把如果是拼接的话,可以用StringBuffer的append方法;
java工程师
其他类似问题
为您推荐:
其他1条回答
你要是想拼接字符串 可以用StringBuffer的append方法
string的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁一、在研究join的用法之前,先明确两件事情。
1.join方法定义在Thread类中,则调用者必须是一个线程,
Thread t = new CustomThread();//这里一般是自定义的线程类
t.start();//线程起动
t.join();//此处会抛出InterruptedException异常
2.上面的两行代码也是在一个线程里面执行的。
以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行
customThread.start();
customThread.join();
在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。
二、为什么要用join()方法
主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
三、join方法的作用
在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:
“等待该线程终止。”
解释一下,是主线程(我在“一”里已经命名过了)等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。(Waits for this thread to die.)
四、用实例来理解
写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。
&1package&wxhx.csdn2;&&&
&2/**&*//**&&
&4&*&@author&bzwm&&
&7class&CustomThread1&extends&Thread&{&&&
&8&&&&public&CustomThread1()&{&&&
&9&&&&&&&&super("[CustomThread1]&Thread");&&&
<span style="color: #&&&&};&&&
<span style="color: #&&&&public&void&run()&{&&&
<span style="color: #&&&&&&&&String&threadName&=&Thread.currentThread().getName();&&&
<span style="color: #&&&&&&&&System.out.println(threadName&+&"&start.");&&&
<span style="color: #&&&&&&&&try&{&&&
<span style="color: #&&&&&&&&&&&&for&(int&i&=&<span style="color: #;&i&&&<span style="color: #;&i++)&{&&&
<span style="color: #&&&&&&&&&&&&&&&&System.out.println(threadName&+&"&loop&at&"&+&i);&&&
<span style="color: #&&&&&&&&&&&&&&&&Thread.sleep(<span style="color: #00);&&&
<span style="color: #&&&&&&&&&&&&}&&&
<span style="color: #&&&&&&&&&&&&System.out.println(threadName&+&"&end.");&&&
<span style="color: #&&&&&&&&}&catch&(Exception&e)&{&&&
<span style="color: #&&&&&&&&&&&&System.out.println("Exception&from&"&+&threadName&+&".run");&&&
<span style="color: #&&&&&&&&}&&&
<span style="color: #&&&&}&&&
<span style="color: #}&&&
<span style="color: #class&CustomThread&extends&Thread&{&&&
<span style="color: #&&&&CustomThread1&t1;&&&
<span style="color: #&&&&public&CustomThread(CustomThread1&t1)&{&&&
<span style="color: #&&&&&&&&super("[CustomThread]&Thread");&&&
<span style="color: #&&&&&&&&this.t1&=&t1;&&&
<span style="color: #&&&&}&&&
<span style="color: #&&&&public&void&run()&{&&&
<span style="color: #&&&&&&&&String&threadName&=&Thread.currentThread().getName();&&&
<span style="color: #&&&&&&&&System.out.println(threadName&+&"&start.");&&&
<span style="color: #&&&&&&&&try&{&&&
<span style="color: #&&&&&&&&&&&&t1.join();&&&
<span style="color: #&&&&&&&&&&&&System.out.println(threadName&+&"&end.");&&&
<span style="color: #&&&&&&&&}&catch&(Exception&e)&{&&&
<span style="color: #&&&&&&&&&&&&System.out.println("Exception&from&"&+&threadName&+&".run");&&&
<span style="color: #&&&&&&&&}&&&
<span style="color: #&&&&}&&&
<span style="color: #}&&&
<span style="color: #public&class&JoinTestDemo&{&&&
<span style="color: #&&&&public&static&void&main(String[]&args)&{&&&
<span style="color: #&&&&&&&&String&threadName&=&Thread.currentThread().getName();&&&
<span style="color: #&&&&&&&&System.out.println(threadName&+&"&start.");&&&
<span style="color: #&&&&&&&&CustomThread1&t1&=&new&CustomThread1();&&&
<span style="color: #&&&&&&&&CustomThread&t&=&new&CustomThread(t1);&&&
<span style="color: #&&&&&&&&try&{&&&
<span style="color: #&&&&&&&&&&&&t1.start();&&&
<span style="color: #&&&&&&&&&&&&Thread.sleep(<span style="color: #00);&&&
<span style="color: #&&&&&&&&&&&&t.start();&&&
<span style="color: #&&&&&&&&&&&&t.join();//在代碼2里,將此處注釋掉&&&
<span style="color: #&&&&&&&&}&catch&(Exception&e)&{&&&
<span style="color: #&&&&&&&&&&&&System.out.println("Exception&from&main");&&&
<span style="color: #&&&&&&&&}&&&
<span style="color: #&&&&&&&&System.out.println(threadName&+&"&end!");&&&
<span style="color: #&&&&}&&&
<span style="color: #}
打印结果:
main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。
[CustomThread1] Thread start.//线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。
修改一下代码,得到代码2:(这里只写出修改的部分)
&1public&class&JoinTestDemo&{&&&
&2&&&&public&static&void&main(String[]&args)&{&&&
&3&&&&&&&&String&threadName&=&Thread.currentThread().getName();&&&
&4&&&&&&&&System.out.println(threadName&+&"&start.");&&&
&5&&&&&&&&CustomThread1&t1&=&new&CustomThread1();&&&
&6&&&&&&&&CustomThread&t&=&new&CustomThread(t1);&&&
&7&&&&&&&&try&{&&&
&8&&&&&&&&&&&&t1.start();&&&
&9&&&&&&&&&&&&Thread.sleep(<span style="color: #00);&&&
<span style="color: #&&&&&&&&&&&&t.start();&&&
<span style="color: #//&&&&&&&&&&t.join();//在代碼2里,將此處注釋掉&&&
<span style="color: #&&&&&&&&}&catch&(Exception&e)&{&&&
<span style="color: #&&&&&&&&&&&&System.out.println("Exception&from&main");&&&
<span style="color: #&&&&&&&&}&&&
<span style="color: #&&&&&&&&System.out.println(threadName&+&"&end!");&&&
<span style="color: #&&&&}&&&
<span style="color: #}
打印结果:
main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);
[CustomThread1] Thread start. //线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。
[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法
在CustomThread的run方法里,执行了t1.join();,进入看一下它的JDK源码:
<span style="color: #public&final&void&join()&throws&InterruptedException&{&&&
<span style="color: #join(<span style="color: #);&&&
<span style="color: #}&&
然后进入join(0)方法:
&1&&&/**&*//**&&
&2&&&&*&Waits&at&most&&code&millis&/code&&milliseconds&for&this&thread&to&&&
&3&&&&*&die.&A&timeout&of&&code&0&/code&&means&to&wait&forever.&//注意这句&&
&5&&&&*&&#64;param&&&&&&millis&&&the&time&to&wait&in&milliseconds.&&
&6&&&&*&&#64;exception&&InterruptedException&if&another&thread&has&interrupted&&
&7&&&&*&&&&&&&&&&&&&the&current&thread.&&The&&i&interrupted&status&/i&&of&the&&
&8&&&&*&&&&&&&&&&&&&current&thread&is&cleared&when&this&exception&is&thrown.&&
&9&&&&*/&&
<span style="color: #&&&public&final&synchronized&void&join(long&millis)&//参数millis为0.&&&
<span style="color: #&&&throws&InterruptedException&{&&&
<span style="color: #long&base&=&System.currentTimeMillis();&&&
<span style="color: #long&now&=&<span style="color: #;&&&
<span style="color: #if&(millis&&&<span style="color: #)&{&&&
<span style="color: #&&&&&&&&&&&throw&new&IllegalArgumentException("timeout&value&is&negative");&&&
<span style="color: #}&&&
<span style="color: #if&(millis&==&<span style="color: #)&{//进入这个分支&&&
<span style="color: #&&&&while&(isAlive())&{//判断本线程是否为活动的。这里的本线程就是t1.&&&
<span style="color: #&&&&wait(<span style="color: #);//阻塞&&&
<span style="color: #&&&&}&&&
<span style="color: #}&else&{&&&
<span style="color: #&&&&while&(isAlive())&{&&&
<span style="color: #&&&&long&delay&=&millis&-&&&&
<span style="color: #&&&&if&(delay&&=&<span style="color: #)&{&&&
<span style="color: #&&&&&&&&break;&&&
<span style="color: #&&&&}&&&
<span style="color: #&&&&wait(delay);&&&
<span style="color: #&&&&now&=&System.currentTimeMillis()&-&&&&
<span style="color: #&&&&}&&&
<span style="color: #}&&&
<span style="color: #&&&}&
单纯从代码上看,如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。
二、为什么要用join()方法
主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
????????????????????
感觉这段很成问题&&&&
&re: 浅析 Java Thread.join()
t.start();//线程起动t.join();//此处会抛出InterruptedException异常上面这段代码,不会异常啊。&&&&
&re: 浅析 Java Thread.join()[未登录]
就像主线程是开会一样,会议(主线程)正在进行中,这时候需要一个人join进来(子线程启动),并执行他自己的操作,如果他的操作没有执行完毕,则会议(主线程)不能结束。&&&&
&re: 浅析 Java Thread.join()
&#64;test比如:你准备洗澡,需要准备的步骤,准备好衣服,沐浴的东西及烧水这些事情,由于烧水耗时太长,如果也放在主线程之中,就很浪费资源,所以如果我们另开线程去处理,就会达到很好效果,于是乎在准备好衣服,沐浴的东西之前就去开子线程烧水,烧水的过程中主线程准备好衣服,沐浴的东西,此时就等待水烧好,然后方可痛快的洗澡了!!&&&&
&re: 浅析 Java Thread.join()
&#64;minelibra这个解释不错哟&&&&
&re: 浅析 Java Thread.join()[未登录]
这个简单的东西,你搞那么复杂。主线程等待调用join方法的子线程执行结束后再继续执行&&&&
&re: 浅析 Java Thread.join()[未登录]
&#64;Mark说的对&&&&
&re: 浅析 Java Thread.join()
什么问题?&#64;test&&&&
&re: 浅析 Java Thread.join()[未登录]
你和朋友一起吃饭,突然你肚子痛,要拉屎,这个时候你去了厕所拉屎,拉了很久,但是你的朋友们要等你拉完回来在一起AA&&&&
&re: 浅析 Java Thread.join()
&re: 浅析 Java Thread.join()
&#64;YY这..... 这个知识点 我懂了&&&&
&re: 浅析 Java Thread.join()
麻烦楼主把文章理一理,这么混乱不清还写篇文章,服了。&&&&
&re: 浅析 Java Thread.join()
别人的文章,论文章排版,整洁干净,论解释,逻辑清晰,前后连贯。&&&&
&re: 浅析 Java Thread.join()
&#64;reiike这两个文章不是一样吗?&&&&
&re: 浅析 Java Thread.join()[未登录]
&#64;YY这个解释口味好重&&&&<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 string.join 的文章

 

随机推荐