Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: thread

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy thread

    i want to create two thread one thread will print odd number and another thread will print even number. but the number should be in a sequence like
    1 2 3 4 5 6 7 8 9 10
    help me.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: thread

    First of all you should create 2 threads, either pass a runnable to either of them or use annonymous sub-classes of thread that overwrite their run() method.
    Have them print numbers inside a for loop.

    Then think of a good way to synchronize their actions, perhaps a lock, or an atomic counter. Choice is yours.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: thread

    yea i already did that .but i am stuck in the next part means synchronization of method.please help me to write that code.


    actually i wrote the below code.
    package com.java.demo;


    public class Practice1
    {
    public static void main(String [] args)
    {
    Thread t2=new MyThread1();
    t2.start();
    Thread t1=new MyThread();
    t1.start();
    }
    }
    class MyThread1 extends Thread
    {
    public void run()
    {
    for(int i=1;i<=10;i+=2)
    {
    System.out.println(i);
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    class MyThread extends Thread
    {
    public void run()
    {
    for(int j=2;j<=10;j+=2)
    {
    System.out.println(j);
    try {
    Thread.sleep(500);
    } catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    }
    }

    but i am getting o/p-
    1
    2
    3
    4
    6
    5
    7
    8
    10
    9
    i want o/p as-
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: thread

    What methods for synchronization do you know and which do you want to use?

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    22
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: thread

    if we write synchronized before any method it will get synchronized. but in this program what is the solution i am not getting.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: thread

    I meant other ways of synchronization, there is more then one.
    Maybe google "consumer producer synchronization" and read up on some documentations and articles describing what it is.

Similar Threads

  1. Exception in thread "Thread-0" .. Please help me!
    By kateyoz in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 7th, 2014, 04:13 PM
  2. How to categorise a daemon thread to specific thread in java?
    By rajasekhar_b in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2014, 06:55 AM
  3. Replies: 1
    Last Post: September 24th, 2013, 04:18 PM
  4. Creating a Thread from a Thread
    By angstrem in forum Threads
    Replies: 11
    Last Post: May 29th, 2013, 09:31 AM
  5. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM