Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 已同步两个线程未同步工作_Java_Multithreading_Synchronized - Fatal编程技术网

Java 已同步两个线程未同步工作

Java 已同步两个线程未同步工作,java,multithreading,synchronized,Java,Multithreading,Synchronized,考虑以下代码: public class Main { public static void main(String[] args){ MyObject obj = new MyObject(); Thread1 t1 = new Thread1(100,'#',obj); Thread1 t2 = new Thread1(100,'*',obj); t1.start(); t2.start();

考虑以下代码:

public class Main {

    public static void main(String[] args){
        MyObject obj = new MyObject();
        Thread1 t1 = new Thread1(100,'#',obj);
        Thread1 t2 = new Thread1(100,'*',obj);

        t1.start();
        t2.start();
    }
}

公共类Thread1扩展线程{
int myNum;
char-myChar;
肌体肌体;
公共线程1(整数、字符c、MyObject对象){
myNum=num;
myChar=c;
myObj=obj;
}
公共同步的无效运行(){

for(int i=1;i同步锁定对象的监视器。在您的情况下,您正在针对每个线程对象进行同步,即线程A锁定线程A的监视器,线程B锁定线程B的监视器。因此,它们不进行交互

对于run()方法,我认为您的意思是:

致:

编辑另一种方法,该方法不锁定myObj,而是锁定Thread1的类实例

public void run(){
    doJob();
}

private static synchronized void doJob() {
    for(int i = 1; i<myNum; i++){
        if((i%10)==0)
            System.out.println("");
        System.out.print(myChar);
    }
}
public void run(){
doJob();
}
私有静态同步void doJob(){

for(int i=1;i同步锁定对象的监视器。在您的情况下,您正在针对每个线程对象进行同步,即线程A锁定线程A的监视器,线程B锁定线程B的监视器。因此,它们不进行交互

对于run()方法,我认为您的意思是:

致:

编辑另一种方法,该方法不锁定myObj,而是锁定Thread1的类实例

public void run(){
    doJob();
}

private static synchronized void doJob() {
    for(int i = 1; i<myNum; i++){
        if((i%10)==0)
            System.out.println("");
        System.out.print(myChar);
    }
}
public void run(){
doJob();
}
私有静态同步void doJob(){

对于(inti=1;i,您可以尝试放置thread.join(),等待执行中的第一个线程完成以启动下一个线程

public class hilos {
    public static void main(String[] args) throws InterruptedException{

        MyObject obj = new MyObject();
        Thread1 t1 = new Thread1(50,'#',obj);
        Thread1 t2 = new Thread1(50,'*',obj);

        Thread[] threads = {t1, t2}; 


        start(threads);


    }


    public synchronized static void start(Thread[] threads) throws InterruptedException{

        synchronized(threads){
            for(int i=0; i < threads.length; i++)   {
                threads[i].start();
                threads[i].join();
            }
        }

    }
}

您可以尝试放置线程。join(),等待执行中的第一个线程完成,然后启动下一个线程

public class hilos {
    public static void main(String[] args) throws InterruptedException{

        MyObject obj = new MyObject();
        Thread1 t1 = new Thread1(50,'#',obj);
        Thread1 t2 = new Thread1(50,'*',obj);

        Thread[] threads = {t1, t2}; 


        start(threads);


    }


    public synchronized static void start(Thread[] threads) throws InterruptedException{

        synchronized(threads){
            for(int i=0; i < threads.length; i++)   {
                threads[i].start();
                threads[i].join();
            }
        }

    }
}

“不工作”不是一个问题描述。描述它应该做什么。这是两个不同的线程独立地执行它们的
run()
方法。
synchronized()
没有出现在图片中。Rohit Jain为什么同步的方法没有出现在图片中?我将此运行方法设置为synchronized“不工作”不是问题描述。请描述它应该做什么。这是两个不同的线程独立执行其
run()
方法。
sychronized()
没有出现在图片中。Rohit Jain为什么synchronized没有出现在图片中?我将此run方法设置为synchronized我明白你的意思,但是如果我只想通过将整个方法设置为synchronized而不使用synchronized(myObj)来获得相同的结果我该怎么做?谢谢这意味着两个线程共享同一个Thread对象实例。事实并非如此。您可以玩其他游戏在共享实例上获取方法,例如向MyObject添加同步方法或使用静态方法。同步的静态方法会锁定CLAs对象,该对象将在两个线程之间共享。但是run()方法不能声明为静态,因此您必须再次跳过几个环才能使类似的工作正常。我明白您的观点,但如果我只想通过将整个方法设置为synchronized而不使用synchronized(myObj)来获得相同的结果我该怎么做?谢谢这意味着两个线程共享同一个Thread对象实例。事实并非如此。您可以玩其他游戏在共享实例上获取方法,例如向MyObject添加同步方法或使用静态方法。同步的静态方法会锁定CLA但是run()方法不能被声明为静态的,因此您必须再次跳过几个环才能使类似的工作正常。
public void run(){
    doJob();
}

private static synchronized void doJob() {
    for(int i = 1; i<myNum; i++){
        if((i%10)==0)
            System.out.println("");
        System.out.print(myChar);
    }
}
public class hilos {
    public static void main(String[] args) throws InterruptedException{

        MyObject obj = new MyObject();
        Thread1 t1 = new Thread1(50,'#',obj);
        Thread1 t2 = new Thread1(50,'*',obj);

        Thread[] threads = {t1, t2}; 


        start(threads);


    }


    public synchronized static void start(Thread[] threads) throws InterruptedException{

        synchronized(threads){
            for(int i=0; i < threads.length; i++)   {
                threads[i].start();
                threads[i].join();
            }
        }

    }
}
#########
##########
##########
##########
##########*********
**********
**********
**********
**********