Results 1 to 4 of 4
  1. #1
    mukeshgulia is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Lightbulb print pattern as "1 a 2 b 3 c 4 d 5 e" from following program

    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);
                     }
            }
    }
    Please help with sync in this prog.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    wilsonshamim is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    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();
    }
    }

  4. #4
    nothing1010 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  1. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 12:46 AM
  2. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  5. project about "network print spooler"
    By rakesh in forum Networking
    Replies: 0
    Last Post: 01-29-2008, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •