public class MutiThread extends Thread { private byte[] lock = new byte[0]; private int i; private MutiThread(int i) {
this.i = i;
} public void run() {
synchronized (lock) {
if (i == 1)
System.out.println("A");
if (i == 2)
System.out.println("B");
if (i == 3)
System.out.println("C");
}
} public static void main(String[] args) {
MutiThread mutiThread1 = new MutiThread(1);
mutiThread1.start();
MutiThread mutiThread2 = new MutiThread(2);
mutiThread2.start();
MutiThread mutiThread3 = new MutiThread(3);
mutiThread3.start();
}}我想要程序按照 A B C的顺序输出,加上了synchronized关键字也没效果。请解答~javasynchronized
解决方案 »
- springsecurity错误
- 求教各位大侠如何学习JAVAEE..............
- 如何利用 java api 实现笔记本中的撤销功能?
- struts中如何用factory的方式让页面提交list到Action
- RTI DDS运行HelloWorld时出现100个错误,大都是某某软件包不存在和找不到错误
- 关于hibernate查询删除
- 常见错误java.lang.IllegalStateException求助!
- 如何 不使用 server.xml文件配置Tomcat自带连接池?
- Jbuilder运行JSP时出错,
- 求朋友们指点
- replaceAll 替换问题
- activemq生产者和消费者的关系是怎么确定的?
还有你的方法其实达不到同步效果,除非你把private byte[] lock = new byte[0]; 改成
private static byte[] lock = new byte[0];
第三个打印C,使用线程同步,分别依次循环在屏幕上打印19个A,B,C
如ABCABCABCABCABCABCABCABC....
{
public static void main(String[] args)
{
MyThread myThread1 = new MyThread();
myThread1.start();
MyThread myThread2 = new MyThread();
myThread2.start();
MyThread myThread3 = new MyThread();
myThread3.start();
}
}class MyThread extends Thread
{
private static int i = 1;
private static Object object =new Object();
@Override
public void run()
{
synchronized (object)
{
while(i<=19)
{
if(i%3 ==1)
{
System.out.println("A");
}else if (i%3 ==2)
{
System.out.println("B");
}else
{
System.out.println("C");
}
i++;
}
}
}
}
你现在每个线程都会生成一个lock对象,肯定不对
把以下:synchronized (lock) {
if (i == 1)
System.out.println("A");
if (i == 2)
System.out.println("B");
if (i == 3)
System.out.println("C");
}改成:synchronized (MutiThread.class) {
if (i == 1)
System.out.println("A");
if (i == 2)
System.out.println("B");
if (i == 3)
System.out.println("C");
}即可
如果要实现让线程按照一定的顺序,我的想法是,在外面定义一个int变量,通过这个变量来判断哪个线程应该等待,哪个线程应该执行,某个线程执行完之后,再把int变量设置成下个线程所对应的变量。
class Printer implements Runnable{
private static Object lock=new Object();
private static int current=0; private String content=null;
private int flag=0;
public Printer(String content,int flag){
this.content=content;
this.flag=flag;
}
public void run{
int times=0;
while(times<19){
synchronized (lock) {
while(current%3!=flag){
try{
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(content);
times++;
Runnable.current++;
lock.notifyAll();
}
}
}}
public class ThreadTest
{
public static void main(String[] args)
{
new Thread(new Printer("A",0)).start();
new Thread(new Printer("B",1)).start();
new Thread(new Printer("C",2)).start();
}
}
class Printer implements Runnable {
private static Object lock = new Object();
private static int current = 0; private String content = null;
private int flag = 0; public Printer(String content, int flag) {
this.content = content;
this.flag = flag;
} public void run() {
int times = 0;
while (times < 19) {
synchronized (lock) {
while (current % 3 != flag) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(content);
times++;
Printer.current++;
lock.notifyAll();
}
}
}}public class ThreadTest {
public static void main(String[] args) {
new Thread(new Printer("A", 0)).start();
new Thread(new Printer("B", 1)).start();
new Thread(new Printer("C", 2)).start();
}
}12楼写的很好,十分感谢~结贴
private static int i = 1;
private final char[] CHARS = {'A', 'B', 'C'};
private final int local_i;
MyThread(int param){
local_i = param;
}
public void run(){
while(true){
if(i == local_i){
System.out.println(CHARS[i - 1]);
i++;
break;
}else{
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
} public static void main(String[] args) {
MyThread myThread1 = new MyThread(1);
myThread1.start();
MyThread myThread2 = new MyThread(2);
myThread2.start();
MyThread myThread3 = new MyThread(3);
myThread3.start();
}
}