遇到一个问题
2个class
ThreadA会打印出6行:每行间隔100ms
1,Thread A
2,Thread A
3,Thread A
4,Thread A
5,Thread A
6,Thread A
ThreadB也会打印出六行:每行间隔100ms
1,Thread B
2,Thread B
3,Thread B
4,Thread B
5,Thread B
6,Thread B如何使得两个线程有序的打印出来? 先打印完A 在打印B
我想用到synchronised Syste。out 但是具体怎么使用不知道 求解。
2个class
ThreadA会打印出6行:每行间隔100ms
1,Thread A
2,Thread A
3,Thread A
4,Thread A
5,Thread A
6,Thread A
ThreadB也会打印出六行:每行间隔100ms
1,Thread B
2,Thread B
3,Thread B
4,Thread B
5,Thread B
6,Thread B如何使得两个线程有序的打印出来? 先打印完A 在打印B
我想用到synchronised Syste。out 但是具体怎么使用不知道 求解。
解决方案 »
- java Swing 打包问题
- 【讨论】读写文件的效率
- eclipse中使用JNI需要额外配置么? 遇到一个程序执行问题, 请帮忙!
- 非public类的static变量的访问问题
- java 要实现这种数据结构用什么类比较好
- 装过oracle后,java程序不能运行
- 微软_编程精粹下载!!!晚了没了------------------
- 按钮事件 和 数据报发送问题
- 在linux下可否用 VC?在linux下如何可以用Java吗?能做哪些事情?
- java jPasswordField密码框输入密码后报错An uncaught exception was raised
- 关于CellEditorListener编辑监控器的问题。。。进进进
- short a = 1;当执行a += 2;时,为什么不会提示精度损失?
请问如何使用wait notify的方法啊? 如果大家好心可以给点代码么? 看例子可能我会比较容易理解一点
package com.ssj.test;public class NotifySynch {
private Object lock = new Object();
class ThreadA extends Thread{
@Override
public void run() {
for(int i = 0 ; i < 6 ; i++){
System.out.println("ThreadA");
}
synchronized(lock){
lock.notifyAll();
}
}
}
class ThreadB extends Thread{
@Override
public void run() {
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e) {
}
}
for(int i = 0 ; i < 6 ; i++){
System.out.println("ThreadB");
}
}
}
public void testRun(){
new ThreadB().start();
new ThreadA().start();
}
public static void main(String[] args) {
new NotifySynch().testRun();
}
}
public class ThreadA implements Runnable { private int i;
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(System.out){
i=2;
System.out.printf("thread A:\n"); while(i<7){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.printf(i+", thread A\n");
i++;
}
}
notify();}
}
package TD;public class ThreadB extends Thread { private int i;
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(System.out){
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
i=2;
System.out.printf("thread B:\n"); while(i<7){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.printf(i+", thread B\n");
i++;
}
}
}}package TD;public class Main { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadA ta=new ThreadA();
ThreadB tb=new ThreadB();
Thread t=new Thread(ta);
t.start();
tb.start();
}
}
Semaphore sem ;
public SemaphoreTest(){
sem = new Semaphore(0);
}
class ThreadA extends Thread{
@Override
public void run() {
for(int i = 0 ; i < 6 ; i++){
System.out.println("ThreadA");
}
sem.release();
}
}
class ThreadB extends Thread{
@Override
public void run() {
try {
sem.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0 ; i < 6 ; i++){
System.out.println("ThreadB");
}
}
}
public void testRun(){
new ThreadB().start();
new ThreadA().start();
}
public static void main(String[] args) {
System.out.println("use SemaphoreTest");
new SemaphoreTest().testRun();
}
}
public class ThreadA implements Runnable { private int i;
@Override
public void run() {
// TODO Auto-generated method stub
i=2;
System.out.printf("thread A:\n"); while(i<7){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.printf(i+", thread A\n");
i++;
}
synchronized(System.out){
System.out.notify();
}
}
}
改threada 感谢帮助