I want to create a new thread in a program that i'm working on but i cant get my new threads to start
does anyone have some code that would allow me to create and start new threads

Lets see yours, and we'll help you fix it, otherwise see this.

I just need to know how to use this

class NewThread implements Runnable
{
    Thread t;
    NewThread ()
    {
        // Create a new, second thread
        t = new Thread (this, "Demo Thread");
        System.out.println ("Child thread: " + t);
        t.start (); // Start the thread
    }
    public static void main (String [] args)
    {
      
    }

    // This is the entry point for the second thread.
    public void run ()
    {
        try
        {
            for (int i = 5 ; i > 0 ; i--)
            {
                System.out.println ("Child Thread: " + i);
                // Let the thread sleep for a while.
                Thread.sleep (500);
            }
        }
        catch (InterruptedException e)
        {
            System.out.println ("Child interrupted.");
        }
        System.out.println ("Exiting child thread.");
    }
}

class ThreadDemo
{
    public static void main (String args[])
    {
        new NewThread (); // create a new thread
        try
        {
            for (int i = 5 ; i > 0 ; i--)
            {
                System.out.println ("Main Thread: " + i);
                Thread.sleep (1000);
            }
        }
        catch (InterruptedException e)
        {
            System.out.println ("Main thread interrupted.");
        }
        System.out.println ("Main thread exiting.");
    }
}

Use it for what? It doesn't look useful for anything to me.

that code is useless but I can replace all the print stuff and make it useful

So what is your question? The code you posted does work, it just isn't useful for anything. So what exactly are you trying to figure out?

There are a ton of references on basic thread usage. Perhaps you need to read them? Or ask a specific question that someone could actually help you out with.

I just need to know how to use this

class NewThread implements Runnable
{
    Thread t;
    NewThread ()
    {
        // Create a new, second thread
        t = new Thread (this, "Demo Thread");
        System.out.println ("Child thread: " + t);
        t.start (); // Start the thread
    }
    public static void main (String [] args)
    {
      
    }

    // This is the entry point for the second thread.
    public void run ()
    {
        try
        {
            for (int i = 5 ; i > 0 ; i--)
            {
                System.out.println ("Child Thread: " + i);
                // Let the thread sleep for a while.
                Thread.sleep (500);
            }
        }
        catch (InterruptedException e)
        {
            System.out.println ("Child interrupted.");
        }
        System.out.println ("Exiting child thread.");
    }
}

class ThreadDemo
{
    public static void main (String args[])
    {
        new NewThread (); // create a new thread
        try
        {
            for (int i = 5 ; i > 0 ; i--)
            {
                System.out.println ("Main Thread: " + i);
                Thread.sleep (1000);
            }
        }
        catch (InterruptedException e)
        {
            System.out.println ("Main thread interrupted.");
        }
        System.out.println ("Main thread exiting.");
    }
}

By calling new Thread(Runnable) and start(). See the linked tutorial and at least try.

And, in fact, that code does that already (not that I really agree with that constructor, but hey, to each his own). Now you want us to provide more code to show you how to use code that already does what you want us to show you? If you haven't been able to get that out of studying this code, how would us providing more code help?

I agree that the constructor of the NewThread is non-sense... Why would you create a thread and then start it right away. In this case, the thread could have executed the loop inside its own class once before it sleeps in the ThreadDemo class loop...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.