考试题,大家做一下 举例说明线程中sleep方法的使用。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class TestSync{ public static void main(String args[]){ Target t=new Target(); new ThreadA(t).start(); new ThreadB(t).start(); }}class Target{ String msg="<我是线程要访问的对象>"; public void show(){ char []msgchars=msg.toCharArray(); for(int i=0;i<msgchars.length;i++){ System.out.print(msgchars[i]); try{ Thread.sleep(1000); }catch(Exception e){ } } }}class ThreadA extends Thread{ Target target; public ThreadA(Target target){ this.target=target; } public void run(){ synchronized(target){// 同步对象 target.show(); } }}class ThreadB extends Thread{ Target target; public ThreadB(Target target){ this.target=target; } public void run(){ synchronized(target){// 同步对象 target.show(); } }} public class TestSync{ public static void main(String args[]){ Target t=new Target(); new ThreadA(t).start(); new ThreadB(t).start(); }}class Target{ String msg="<我是线程要访问的对象>"; public synchronized void show(){ char []msgchars=msg.toCharArray(); for(int i=0;i<msgchars.length;i++){ System.out.print(msgchars[i]); try{ Thread.sleep(1000); }catch(Exception e){ } } }}class ThreadA extends Thread{ Target target; public ThreadA(Target target){ this.target=target; } public void run(){ target.show(); }}class ThreadB extends Thread{ Target target; public ThreadB(Target target){ this.target=target; } public void run(){ target.show(); }} 当涉及到很多窗口的时候,怎么控制他们的显示跟隐藏啊? 菜鸟跪求帮助 一个线程题目 如果使窗口总在最前? 如何通过代码最小化Frame窗口,java???? Oracle数据库循环调用问题 Java 中 有 "表 "这 种 数 据 结 构 么 ? 怎么才能设置Table 的某个单元格可以被编辑啊! 如何把applet转化成应用程序? 容器使用泛型后,为什么还要强制转换 【新手提问】FileInputStream的读取问题! JAVA基础问题
public static void main(String args[]){
Target t=new Target();
new ThreadA(t).start();
new ThreadB(t).start();
}
}class Target{
String msg="<我是线程要访问的对象>";
public void show(){
char []msgchars=msg.toCharArray();
for(int i=0;i<msgchars.length;i++){
System.out.print(msgchars[i]);
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
}
}class ThreadA extends Thread{
Target target;
public ThreadA(Target target){
this.target=target;
}
public void run(){
synchronized(target){// 同步对象
target.show();
}
}
}
class ThreadB extends Thread{
Target target;
public ThreadB(Target target){
this.target=target;
}
public void run(){
synchronized(target){// 同步对象
target.show();
}
}
}
public static void main(String args[]){
Target t=new Target();
new ThreadA(t).start();
new ThreadB(t).start();
}
}class Target{
String msg="<我是线程要访问的对象>";
public synchronized void show(){
char []msgchars=msg.toCharArray();
for(int i=0;i<msgchars.length;i++){
System.out.print(msgchars[i]);
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
}
}class ThreadA extends Thread{
Target target;
public ThreadA(Target target){
this.target=target;
}
public void run(){
target.show();
}
}
class ThreadB extends Thread{
Target target;
public ThreadB(Target target){
this.target=target;
}
public void run(){
target.show();
}
}