Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CoopOwnz · Oct 22, 2016 at 06:27 PM · optimizationclassesthreading

Start a Thread in Class Constructor

I created an example class to demonstrate what I'm trying to do. Each class should run its calculation on a separate thread.

 using System.Collections;
 using System.Threading;
 
 
 public class PlayAShoe {
 
     private static readonly System.Random random = new System.Random();
     private static readonly object synlock = new object();
 
     private int rnum = 0;
     public int currthread;
     public bool isFinished;
 
     public PlayAShoe(int i)
     {
         isFinished = false;
         currthread = i;
         System.Threading.ParameterizedThreadStart pts = new System.Threading.ParameterizedThreadStart(startShoe);
         System.Threading.Thread waitforone = new System.Threading.Thread (pts);
         waitforone.Start (i);
         //startShoe();
     }
 
     public int getNum()
     {
         return rnum;
     } 
 
     private void startShoe(object threadparams)
     {
         for (int i = 0; i < 5000000; i++)
         {
             rnum += RandomNumber(0, 10);
         }
 
         isFinished = true;
     }
 
     public static int RandomNumber(int min, int max)
     {
         lock (synlock) 
         {
             return random.Next(min, max);
         }
     }
 }

In my controller script I make 4 of these classes. And print the result in the update function once they are finished:

 public class SimulationController : MonoBehaviour {
     PlayAShoe newshoe1;
     PlayAShoe newshoe2;
     PlayAShoe newshoe3;
     PlayAShoe newshoe4;

 void Start () 
     {
         newshoe1 = new PlayAShoe(1);
         newshoe2 = new PlayAShoe(2);
         newshoe3 = new PlayAShoe(3);
         newshoe4 = new PlayAShoe(4);
     }

 void Update () 
     {
         if (newshoe1.isFinished)
         {
             print(newshoe1.getNum() + " " + newshoe1.currthread);
             newshoe1.isFinished = false;
         }
         if (newshoe2.isFinished)
         {
             print(newshoe2.getNum() + " " + newshoe2.currthread);
             newshoe2.isFinished = false;
         }
         if (newshoe3.isFinished)
         {
             print(newshoe3.getNum() + " " + newshoe3.currthread);
             newshoe3.isFinished = false;
         }
         if (newshoe4.isFinished)
         {
             print(newshoe4.getNum() + " " + newshoe4.currthread);
             newshoe4.isFinished = false;
         }
 
     }
 }

I understand how to do this without using a separate class but eventually the calculation done in PlayAShoe will have many lists and variables used in the long calculation making a class the most useful way to do it. The problem is I need to make it faster so I thought I could split this up by making several classes run the long calculation on separate threads and just grab the results when they're done. The problem is it is actually much faster when I don't create the thread in the constructor and just do a function call instead. Why is this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Most efficient way to find the closest collider to a player? 1 Answer

Was the audio file embed into the scene asset bundle file? 0 Answers

24m tri 50m verts from 300 trees 2 Answers

Optimization and Good habits programming whit C# 1 Answer

Increase FPS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges