저는 Java 프로그래밍이 처음이므로 스레드 생성자에서 정수 값을 매개 변수로 전달하려고합니다. 누구든지 나를 도울 수 있습니까? 내 코드는 다음과 같습니다 :java의 스레드 생성자에서 매개 변수로 int 값을 전달합니다.
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);
의.
왜 당신도 그것을 사용하려고하지 않는 경우 실행 가능한 클래스를 생성? 해결 방법은 Runnable의 생성자를 사용하고 Runnable 객체로 Thread를 시작하는 것입니다. 약간의 노력을 기울이면이 문제를 즉시 해결하는 방법을 볼 수있는 기본적인 스레딩 튜토리얼을 읽으십시오. –
코드를 통해 저에게 말할 수 있습니까 ?? –