将int值作为java中线程构造函数中的参数传递

时间:2012-03-24 04:13:17

标签: java multithreading

我是Java编程的新手,我想在线程的构造函数中传递一个整数值作为参数。谁能帮我?我的代码如下所示:

import java.io.*;
import java.lang.Thread;
import java.util.Scanner;

class A implements Runnable {
    int count;
    int a;
    Thread t;

    A(int i) {
        synchronized (this) {
            a = i;
            // count=Integer.parseInt(a);
            count = a;
            t = new Thread(this);
            t.start();
        }
    }

    @Override
    public void run() {
        inc();
    }

    public synchronized void inc() {
        try {
            if (count != 0) {
                synchronized (this) {
                    ++count;
                    System.out.println("Incrementing pre " + count);
                    // String p = String.valueOf(c);
                    // System.out.println("Incrementing in value of p " + p);

                    Thread.sleep(2000);
                    // int cminus = count-1;
                    // --count;
                    new B(count);

                    // String p = String.valueOf(cplus);
                    // System.out.println("Incrementing in p  " + cplus);
                }
            } else {
                System.out.println("Count values cannot be negative");
            }
        }
        catch (InterruptedException e) {
            // ...
        }
    }
}

class B implements Runnable {
    int count;
    int a;
    Thread t;

    B(int i) {
        a = i;
        // count=Integer.parseInt(a);
        count = a;

        t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
        dec();
    }

    public synchronized void dec() {
        try {
            if (count != 0) {
                synchronized (this) {
                    --count;
                    System.out.println("Decrementing first " + count);
                    // String p = String.valueOf(count);
                    // System.out.println("Value of p: " + count);

                    Thread.sleep(1000);
                    // ++count;
                    // System.out.println("p out" + p);
                    new A(count);
                }
            } else {
                System.out.println("Count values cannot be negative");
            }
        } catch (InterruptedException e) {
            // ...
        }
    }
}

class Thread_array extends Thread implements Runnable {
    public static void main(String[] args) throws IOException,
            InterruptedException {
        int z;

        System.out.print("Enter your desired number: ");
        Scanner input = new Scanner(System.in);
        int dj = input.nextInt();
        int[] array = new int[dj];
        for (z = 0; z < array.length; z++) {
            array[z] = 0;
            System.out.print(" " + array[z]);
        }

        System.out.println();
        new B(dj);
        new A(dj);
    }
}

总结一下,我希望能够做到这一点:

   t =  new Thread(this, i);

而不是

   t = new Thread(this);

感谢。

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但每当我看到一个实现Runnable的类,然后产生一个Thread并将当前对象传递给线程时,我会对发生的事情感到非常困惑,尤其是当有共享的状态。为什么不将Runnable的逻辑分成不同的类?

class ThreadTask implements Runnable {
    int count;
    int i;

    public ThreadTask(int i) {
        this.i = i;
        this.count = i;
    }

    @Override
    public void run() {
        inc();
    }

    public synchronized void inc() {
        try {
            if (count != 0) {
                synchronized (this) {
                    ++count;
                    System.out.println("Incrementing pre " + count);
                    // String p = String.valueOf(c);
                    // System.out.println("Incrementing in value of p " + p);

                    Thread.sleep(2000);
                    // int cminus = count-1;
                    // --count;
                    new B(count);

                    // String p = String.valueOf(cplus);
                    // System.out.println("Incrementing in p  " + cplus);
                }
            } else {
                System.out.println("Count values cannot be negative");
            }
        }
        catch (InterruptedException e) {
            // ...
        }

  }
}

然后是A级:

class A {
    Thread t;

    public A(int i) {

        ThreadTask task = new ThreadTask(i);
        t = new Thread(task);
        t.start();
    }
}

答案 1 :(得分:-1)

如果您实际查看the documentation,第一个示例将回答您的 EXACT 问题。

看起来很像这样:

class ThreadThatUsesAnInt extends Thread
{
    private int myIntVariable;

    ThreadThatUsesAnInt(int someInt)
    {
        myIntVariable = someInt;
    }

    public void run()
    {
        /* do stuff */
    }
}

这是一个关于如何解决java问题的方便提示:

如果您对如何使用课程有疑问,请执行以下操作:

java 6 <ClassName>

或......在这种情况下:

java 6 Thread

第一个结果几乎总是javadoc,这基本上是使用java的全部关键。