javascript
Brief description  about Online courses   join in Online courses
View Shristy  Bhati 's Profile

Sync with Runnable Interface

class RunnableThread implements Runnable
{
Thread RunningThread;
static String[] msg = { "I", "love","my","motherland"};
public RunnableThread(String ThreadName)
{
RunningThread = new Thread(this, ThreadName);
// System.out.println(RunningThread.getName());
RunningThread.start();
}
public static void main(String[] args)
{
RunnableThread thread1 = new RunnableThread("thread1:");
RunnableThread thread2 = new RunnableThread("thread2:");
thread1.start();
thread2.start();
}
void randomWait()
{
try
{
Thread.currentThread().sleep((long) (3000 * Math.random()));
}
catch (InterruptedException e)
{
System.out.println("Interrupted!");
}
}
public synchronized void run()
{
ThreadExample.displayList(RunningThreadgetName(), msg);
}
}


public class ThreadExample {
public synchronized void displayList(String name, String list[])
{
for (int i = 0; i < list.length; i )
{
RunnableThread t = (RunnableThread)Thread.currentThread();
t.randomWait();
System.out.println(name list[i]);
}
}
}





Can Anybody please help me out with this program....what is wrong with this code.Please help me with this assignment please....





Asked by Shristy Bhati | Jul 10, 2013 |  Reply now
Replies (0)