मैं जावा धागा नया हूँ। मैं निम्नलिखित कोड में मुख्य थ्रेड से लॉक को थ्रेड को वापस देने में असमर्थ हूं। मुझे अवांछित उत्पादन मिल रहा है क्योंकि मैं धागे को अनलॉक करने में असमर्थ हूं। मैं चाहता हूं कि धागा थ्रेड का उपयोग करके मूल्य में वृद्धि करें (उसके बाद प्रतीक्षा करने की स्थिति में आता है) और मूल्य को प्रिंट करने के बाद, अगले बढ़े हुए मूल्य को प्रिंट करने के लिए लॉक जारी करें।
class Foo implements Runnable
{
public volatile int value=0,i=0;
Thread t=new Thread();
public void method(Thread t)
{
this.t = t;
}
@Override
public synchronized void run()
{
while(i<3)
{
value++;//receive and process ACK
i++;
try
{
System.out.println("im thread here");
wait();
System.out.println("passed wait");
}
catch(InterruptedException ex){
}
System.out.println("im notified");
}//while
//}//sync
}//run method
public int getValue()
{
try
{
Thread.sleep(1000);
}
catch (Exception e) {
System.out.println(e);
}
return value;
}
}//class foo
public class ThreadTest
{
public static int value1,times=0;
public static void main(String[] args)
{
Foo foo=new Foo();
Thread t=new Thread(foo);
foo.method(t);
t.start();
while(times<3)
{
synchronized(t)
{
value1=foo.getValue();
times++;
System.out.println(value1);
System.out.println(t.getState());
try
{
t.notify();
System.out.println("Notify is reached");
}
catch(IllegalMonitorStateException ex)
{
System.out.println("Thread is blocked");
}
}//sync
}//while
}//main
}//mclasss
2 जवाब
क्या आप ऐसा कुछ करने की कोशिश कर रहे हैं? यदि आप वास्तव में प्रतीक्षा / सूचना का उपयोग करना चाहते हैं और Runnable का उपयोग करना चाहते हैं।
मैंने एक प्रतीक्षा खंड जोड़ा है, अन्यथा मुख्य धागा पृष्ठभूमि थ्रेड मूल्य में वृद्धि से पहले समाप्त हो सकता है।
class Foo implements Runnable {
public volatile int value = 0, i = 0;
private Thread backgroundThread;
public void setThread(Thread thread) {
this.backgroundThread = thread;
}
@Override
public void run() {
synchronized (backgroundThread) {
while (i < 2) {
value++;
i++;
backgroundThread.notify();
try {
System.out.println("background thread wait start");
backgroundThread.wait();
System.out.println("background thread notified");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public int getValue() {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
public class ThreadTest {
public static int value1, times = 0;
public static void main(String[] args) {
Foo foo = new Foo();
final Thread thread = new Thread(foo);
foo.setThread(thread);
thread.start();
while (times < 3) {
synchronized (thread) {
value1 = foo.getValue();
times++;
System.out.println(value1);
System.out.println(thread.getState());
thread.notify();
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
या आप थ्रेड का विस्तार कर सकते हैं:
class BackgroundThread extends Thread {
public volatile int value = 0, i = 0;
@Override
public synchronized void run() {
while (i < 2) {
value++;
i++;
notify();
try {
System.out.println("background thread wait start");
wait();
System.out.println("background thread notified");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public int getValue() {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
public class ThreadTest {
public static int value1, times = 0;
public static void main(String[] args) {
BackgroundThread backgroundThread = new BackgroundThread();
backgroundThread.start();
while (times < 3) {
synchronized (backgroundThread) {
value1 = backgroundThread.getValue();
times++;
System.out.println(value1);
System.out.println(backgroundThread.getState());
backgroundThread.notify();
try {
backgroundThread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
यह बहुत स्पष्ट नहीं है कि आप वास्तव में क्या करना चाहते हैं, लेकिन हम यहां मान लेंगे कि आप पृष्ठभूमि में एक धागा चलाना चाहते हैं, जो केवल तभी चलना चाहिए जब उसका स्पैनर (मुख्य सूत्र कहते हैं) इसे अनुमति देता है।
JDK के पास इसके लिए पहले से ही कई उपकरण हैं, निम्न स्तर की प्रतीक्षा () और सूचित {, All} () विधियों पर भरोसा करने की कोई आवश्यकता नहीं है।
ऐसे आदिम का एक उदाहरण CountDownLatch
है। यह एक एक-उपयोग इकाई है जो आपको किसी भी थ्रेड के {{X12}} से पहले निर्दिष्ट करने की अनुमति दे सकती है, क्योंकि उनके लिए ट्रिगर किया जा सकता है।
मल्टीथ्रेड हैंडलिंग कक्षाओं के साथ संयोजन में जो कि जावा 1.5 के रूप में वापस दिखाई दिया, इसका मतलब है कि आप इस तरह से कुछ कर सकते हैं:
// Implementation of a Runnable waiting for the counter to trigger
public final class MyWaitingClass
implements Runnable
{
private final CountDownLatch latch;
public MyWaitingClass(final CountDownLatch latch)
{
this.latch = latch;
}
@Override
public void run()
{
try {
latch.await();
// do whatever is necessary
} catch (InterruptedException e) {
// Argh; interrupted before the latch was released
Thread.currentThread().interrupt();
}
}
}
// In the main class:
final ExecutorService executor = Executors.newSingleThreadPool();
final CountDownLatch latch = new CountDownLatch(1);
final Runnable runnable = new MyWaitingClass(latch);
executor.submit(runnable);
// do whatever is needed; then:
latch.countDown();
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।