-2
Error:
        Exception in thread "Thread-2200" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Unknown Source)
        at problem1.solvinging$solver.distancecalculator(Orienteering.java:107)
        at problem1.solveing$solver.run(Orienteering.java:165)

Class:

public void distancecalculator(char [][]problem  , int distancefound) {

    Point p = new Point();
    LinkedList<Point> q = new LinkedList<>();
    q.add(origin);
    int []x = {1,-1,0,0};
    int []y = {0,0,1,-1};


    dist[0][0] = distancefound;
    while(!q.isEmpty()) {

        p = q.getFirst();
        for(int i = 0; i < 4; i++) {

            int a = p.x + x[i];
            int b = p.y + y[i];

            if(a >= 0 && b >= 0 && a < m && b < n && dist[a][b]==2) {

                dist[a][b] = 1 + dist[p.x][p.y] ;

                if(k>0) {
                    solver s = new solver(newproblem,dist[a][b]);
                    new Thread(s).start();
                }

I am getting the above error in my program while executing it
But the program is still running
How i correct it please help me
Thanks in advance.Suggest new edits if required

Michał Schielmann
  • 1,372
  • 8
  • 17
  • i am getting the above error in my console – user3970513 Aug 24 '14 at 09:09
  • You didn't tell **what** the error is. All you told is **where** it happens. A complete stack trace starts with something like "NullPointerException: foo may not be null" and then tells where the exception happens. We need to know what the exception type and message is. – JB Nizet Aug 24 '14 at 09:11
  • OK. So read the error. It's extremely clear. You're creating too many threads, and the OS doesn't want to let you do that. Threads are a limited resource. You can't start as many threads as you want. Use an ExecutorService, which uses a pool of threads, or fix your algorithm if you think your code shouldn't start many threads. – JB Nizet Aug 24 '14 at 09:17
  • @JBNizet would you please help to edit my code so that i can use ExecutorService – user3970513 Aug 24 '14 at 09:19
  • No. That's your job. – JB Nizet Aug 24 '14 at 09:20
  • Please supply more code - what is `k` what happens next in the loop? – Michał Schielmann Aug 24 '14 at 09:35
  • @MichałSchielmann it 100x100 matrix an i am using BFS search in which one point can be visited by more than once , k is the number of Must visited points if k==0 i got the total distance it is working fine for 10x10 matrix – user3970513 Aug 24 '14 at 09:50
  • @user3970513 why not write it in the question. How should we know this? Does the program really work for 10x10 `problem` array and throws no error? – Michał Schielmann Aug 24 '14 at 09:56
  • yes i think for large array size many threads are generated how to solve this is there any way that if threads generated>500 stop the first 100 threads – user3970513 Aug 24 '14 at 09:58
  • @MichałSchielmann is there any way if threads are generate more than 500 stop the first 100 threads – user3970513 Aug 24 '14 at 10:01
  • @user3970513 see my edited answer for a Semaphore example. It should be what you need. – Michał Schielmann Aug 24 '14 at 10:25

3 Answers3

1

In the code you supplied there is no place where you remove elements from LinkedList<Point> q. The getFirst() method doesn't remove the first element. And probably because of that you've created an infinite loop, that creates new thread in every run.

Try pollFirst() instead.

EDIT AFTER QUESTION EXPLANATION IN COMMENTS

What you can do to limit the number of threads is using java.util.concurrent package i.e. Semaphore. Before you create new thread call acquire mehotd on the semapohre. Add the semapohre object to the solver construcotor, so that when the solver finishes it can call the release() mehtod in that solver object.

Try the code below to see how it would work:

public class Main 
{
    public static void main(String args[])
    {
        new Main().run();
    }

    private void run() 
    {
        Semaphore semaphore = new Semaphore(5, true);
        for (int i = 0; i<6; i++)
        {
            try
            {
                semaphore.acquire(1);
                new TestThread(i, semaphore).start();
            }
            catch (final InterruptedException e) {}

        }
    }

    class TestThread extends Thread
    {
        private Semaphore semaphore;
        private Integer id;

        TestThread(final Integer id, final Semaphore semaphore)
        {
            this.semaphore = semaphore;
            this.id = id;
        }

        @Override
        public void run()
        {
            System.out.println("Thread " + id + " has started.");
            try
            {
                Thread.sleep(5000);
            }
            catch (final InterruptedException e) {}
            System.out.println("Thread " + id + " has stopped.");
            semaphore.release(1);
        }
    }
}

There are 6 threads that need to be run, but the semaphore allows only 5 at a time. So the sixth has to wait. All threads do only one thing - wait for 5sec. So the sixth thread has to wait those 5sec so that one of the other threads can finish, and then it can start.

Output:

Thread 2 has started.
Thread 4 has started.
Thread 5 has started.
Thread 3 has started.
Thread 1 has started.
Thread 4 has stopped.
Thread 2 has stopped.
Thread 6 has started.  //here the sixth thread has started - as soon as the other ones finished.
Thread 1 has stopped.
Thread 3 has stopped.
Thread 5 has stopped.
Michał Schielmann
  • 1,372
  • 8
  • 17
0

Most probably you have infite loop that causes OutOfMemoryError. Check q can be empty.

Gurkan İlleez
  • 1,503
  • 1
  • 10
  • 12
0

You should be cautious when you are creating threads,especially about their number,because they are a LIMITED resource. Here you have a while loop and inside you have a for loop and inside that loop you are creating a Thread. You have no idea how many threads your java program is requesting from the underlying OS, then the OS denies,probably says "HOW ABOUT A NO?" to the JVM,and then JVM gives you that message as OutOfMemoryError . Try optimizing your code.

Sagar D
  • 2,588
  • 1
  • 18
  • 31