0

simply put for a hw problem I need to run a bubble sort with 3 million elements. Do this normally. and then sort 4 individual lists (not nearly accurate but is all thats required) of 750000 elements each on a different thread.

I have a class that extends thread that simply prints a line if im running the thread. But I'm not sure how to get it to run a method

   class thread extends Thread{
   override def run()
   {
   // Displaying the thread that is running
   println("Thread " + Thread.currentThread().getName() +" is running.")

    }
     }    

//this for loop is in an object

for (c <- 1 to 5)
{
  var th = new thread()
  th.setName(c.toString())
  th.start()


}
4
  • Maybe you want to use Future instead of Thread. Commented Aug 14, 2019 at 16:06
  • Re, "not nearly as accurate, but..." Bubble sorting four lists of N/4 elements each takes less time than sorting a single list of N elements even if you only use one thread. You should understand how much less time and why before you attempt to interpret the result of the multi-threaded experiment. Commented Aug 14, 2019 at 18:04
  • Please, format your code according Scala style. Commented Aug 14, 2019 at 20:06
  • you need to know about data to sort. In term of immutable you can define constructor to pass data to object of child class. Sorry for my English Commented Aug 14, 2019 at 20:21

1 Answer 1

1

I am going to stick to answering the question you asked instead of trying to do your homework for you. I hope that is enough to get your started trying things out for yourself.

Recall that class names should be capitalized. That is probably a good thing to remember as your instructor will probably mark you down if you forget that during an exam.

class MyThread extends Thread{
  override def run() = {
    // Displaying the thread that is running
    println("Thread " + Thread.currentThread().getName() +" is running.")

    sort()
  }

  def sort() = {
    // my sorting code here
  }
}  

If your instructor has not restricted you to using Thread only, I would also, similar to Luis Miguel Mejía Suárez, recommend Future instead of Thread. The syntax it uses is cleaner and preferable for how I would do multithreading in a professional setting. However, the choice may not be yours, so if your teacher says use Thread, use Thread.

For a primer on using Futures, see here.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.