• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question in thread

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Cert extends Thread
{
static int x=10;
public static void main( String args[] ) throws Exception
{
Cert Cert = new Cert();
Thread t1 = new Thread(Cert);
Thread t2 = new Thread(Cert);
t1.start();
t2.start();
}

public synchronized void run()
{
System.out.print(++x + " ");;
}
}


In the above program can the Output be guaranteed ???.........I think the output can be guaranteed as Both the Thread are running on different Objects......Am i Right ??
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sagar shroff wrote:public class Cert extends Thread
{
static int x=10;
public static void main( String args[] ) throws Exception
{
Cert Cert = new Cert();
Thread t1 = new Thread(Cert);
Thread t2 = new Thread(Cert);
t1.start();
t2.start();
}

public synchronized void run()
{
System.out.print(++x + " ");;
}
}


In the above program can the Output be guaranteed ???.........I think the output can be guaranteed as Both the Thread are running on different Objects......Am i Right ??





Or Do the two Threads run on the same Object ...Please Help ??....
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sagar shroff wrote:





Well, you passed the same runnable Cert to both the threads and the run() method is synchronized.

See if this code helps :



OUTPUT :
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sagar...Please always use code tags when writing programs...it makes it easier to analyse, you will find it at the top of the editor when creating your posts...
If you extend a Thread, override the run() method, pass 'a-Thread' or 'a-Runnable' to the threads constructor and invoke the start() method on the Thread Object, the 'new-thread-of-execution' is guaranteed to start and run to completion, provided there is NO other circumstance that alter the normal flow of the program.

Regards

Ikpefua
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rahul......and Jacob yeah I will take care next time...nd thnks.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic