Results 1 to 4 of 4
- 12-30-2010, 06:05 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
print pattern as "1 a 2 b 3 c 4 d 5 e" from following program
Please help with sync in this prog.Java Code:class thread1 implements Runnable { public void run() { try { for(int i=1;i<=5;i++) { System.out.println(i); } } catch(Exception e) { System.out.println(e); } } } class thread2 implements Runnable { char [] alphabets={'a','b','c','d','e'}; public void run() { try { for(int i=0;i<5;i++) { System.out.println(alphabets[i]); } } catch(Exception e) { System.out.println(e); } } } public class threadfinal { public static void main(String [] args) { thread1 t1=new thread1(); thread2 t2=new thread2(); Thread th1=new Thread(t1,""); Thread th2=new Thread(t2,""); try { th1.start(); th2.start(); } catch(Exception e) { System.out.println(e); } } }
- 12-30-2010, 07:41 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Both threads need to synchronize on a shared object (a lock). They should wait until the other thread notifies them and change roles, i.e. the other thread should wait and the first thread should notify it; before notifying the other thread the thread should print its character; but this entire scenario would be a waste of resources because you can easily avoid this synchronized behaviour in a single thread.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 07:11 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Hi ,
Call another thread from the first run() method something like this
class myPrintClass implements Runnable
{
char[] x = {'a','b','c','d','e','f','g','h','i','j'};
@Override
public void run() {
// TODO Auto-generated method stub
for(int i =0; i< 10;i++)
{
System.out.println(i);
myprintAplha ma = new myprintAplha(x[i]);
Thread t2 = new Thread(ma);
t2.start();
}
}
}
class myprintAplha implements Runnable
{
private char cc;
public myprintAplha(char c)
{
cc = c;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(cc);
}
}
public class print1a {
public static void main(String[] s) throws InterruptedException
{
myPrintClass mp = new myPrintClass();
Thread t1 = new Thread(mp);
t1.start();
}
}
- 12-31-2010, 08:25 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
I think you should do that: :D
PHP Code:class _Queue { int i = -1; boolean busy = false; synchronized void set(int i) { if(!busy) { try { wait(); } catch(InterruptedException e) { } } busy = false; this.i = i; notifyAll(); } synchronized int get() { if(busy) { try { wait(); } catch(InterruptedException e) { } } busy = true; notifyAll(); return i; } } class thread1 implements Runnable { _Queue q; thread1(_Queue q) { this.q = q; } public void run() { for(int i=0;i<5;i++) { System.out.println(i); q.set(i); try { Thread.sleep(300); } catch(InterruptedException e) { } } } } class thread2 implements Runnable { char [] alphabets={'a','b','c','d','e'}; _Queue q; thread2(_Queue q) { this.q = q; } public void run() { int j; while(true) { j = q.get(); if (j != -1) System.out.println(alphabets[j]); if (j == 4) break; } } } public class threadfinal { public static void main(String [] args) { _Queue q = new _Queue(); thread1 t1=new thread1(q); thread2 t2=new thread2(q); Thread th1=new Thread(t1,"1"); Thread th2=new Thread(t2,"2"); th1.start(); th2.start(); try { th1.join(); th2.join(); } catch(InterruptedException e) { } } }
Similar Threads
-
How "Pattern.matches(regex, input)" function works?
By kishan in forum Advanced JavaReplies: 2Last Post: 04-26-2009, 12:46 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
project about "network print spooler"
By rakesh in forum NetworkingReplies: 0Last Post: 01-29-2008, 07:48 AM


LinkBack URL
About LinkBacks

Bookmarks