简体   繁体   English

在java中定义两个不同的线程

[英]Defining two different Threads in java

Im new to Threads and I was wondering how could I define what two or more different Threads do in a Java program.我是线程的新手,我想知道如何定义两个或多个不同线程在 Java 程序中的作用。 Do i define them all in the same public void run method?我是否在同一个 public void run 方法中定义它们? If so, how do I do it?如果是这样,我该怎么做? I would like the Threat t1 to invoke the increment method, t2 to invoke the decrement method and both of them to call the value method我希望威胁 t1 调用 increment 方法,t2 调用 decrement 方法,并且两者都调用 value 方法

Here's the code example:这是代码示例:

package interference;

/**
*
* @author rodrigopeniche
*/
public class Interference implements Runnable{

/**
 * @param args the command line arguments
 * 

 */

Counter counter1= new Counter();

class Counter{

    private int c= 0;

    public void increment()
    {
        c++;
    }

    public void decrement()
    {
        c--;
    }

    public int value()
    {
        return c;
    }

}

public static void main(String[] args) {
    // TODO code application logic here


 Thread t1= new Thread(new Interference());
 Thread t2= new Thread(new Interference());
 t1.start();
 t2.start();

}



@Override
public void run() {

counter1.increment();
counter1.decrement();
counter1.value();



}

}

You can set names to threads like thread1, thread2 .您可以将名称设置为thread1, thread2类的线程。 After that, in the run method, check the name of the thread currently running and do the necessary action.之后,在 run 方法中,检查当前正在运行的线程的名称并执行必要的操作。

You have to add a while loop inside the run method if you need to run it longer.如果您需要更长时间地运行它,则必须在 run 方法中添加一个 while 循环。

public static void main(String[] args) {

    Interference interference = new Interference();//create a new Interference object

    Thread t1 = new Thread(interference, "thread1");//pass the runnable interference object and set the thread name
    Thread t2 = new Thread(interference, "thread2");//pass the runnable interference object and set the thread name
    t1.start();
    t2.start();
}

@Override
public void run() {

    while (true) {//to run it forever to make the difference more visual

        String threadName = Thread.currentThread().getName();//get the current thread's name
        if (threadName.equals("thread1")) {//if current thread is thread1, increment
            counter1.increment();
        } else if (threadName.equals("thread2")) {//if current thread is thread2, decrement
            counter1.decrement();
        }

        System.out.println(counter1.value());//print the value
    }
}

When you run the code, you can see count is going up and down in a random manner.当您运行代码时,您可以看到计数以随机方式上升和下降。

In your current code, counter1 is an instance variable of class Interference .在您当前的代码中, counter1是类Interference的实例变量。 You create 2 instances of Interference and then use them to create two Thread objects.您创建 2 个Interference实例,然后使用它们创建两个Thread对象。 When the threads start to run, each Thread is actually working on it's own copy of counter1 .当线程开始运行时,每个Thread实际上都在处理它自己的counter1副本。 I think that may not be what you expect.我认为这可能不是您所期望的。

package interference;

public class Interference {
    static class Counter {
        private int c = 0;

        public void increment() {
            c++;
        }

        public void decrement() {
            c--;
        }

        public int value() {
            return c;
        }
    }

    public static void main(String[] args) {
        Counter counter = new Counter();

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                counter.increment();
                System.out.println(counter.value());
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                counter.decrement();
                System.out.println(counter.value());
            }
        });
        t1.start();
        t2.start();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM