• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by miningold · Sep 11, 2012 at 01:11 AM · networkingmultithreadingasynchronousandroidjni

AndroidJNI attaching to multiple threads

I am creating multiple threads in Unity with C# (I know, I know, Unity isn't built for multithreading).

I have a Java function that simply returns the number 3. On the C# side if I call the Java function on the main Unity thread then I get the number 3 returned:

 int foo = javaObject.Call<int>("foo"); 
 // foo == 3

Now if I call the same Java function from a thread I created in C# then I get the number 0 returned:

 new Thread(delegate() {
     AndroidJNI.AttachCurrentThread();
     int foo = javaObject.Call<int>("foo");
     // foo == 0
 }).Start();

The reason I am using threading is because I am sending and receiving packets and I don't want both ends to be stuck on receiving and not being able to send, essentially creating deadlock.

I am using AndroidJNI because the library I am using for networking was built for Android using Java. I have to use this library for this project.

I am fairly new to JNI and so I might be missing some steps. I know I have the synchronous method setup properly (not all setup steps are shown). But please let me know if there are more steps required for using multiple threads.

Let me know if you need any additional information.

Comment
Add comment · Show 1
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
avatar image miningold · Sep 10, 2012 at 11:19 PM 0
Share

Note: An alternate solution to this would be to have the threads running in Java then passing the received data to C#. However, currently it is only possible to send strings from Java to C#. A question regarding this subject has been posted here http://answers.unity3d.com/questions/314271/will-unity-40-support-sending-complex-data-from-an.html#comment-315848

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BGawenda · Aug 19, 2013 at 02:14 PM

I know this post is rather old, but I just ran into the same kind of problem using Unity 4 When I try to create at AndroidJavaObject from a different thread it throws exception. Although I first called the AndroidJNI.AttachCurrentThread method. Is there any update on this issue? Or is it just not supported to call into JNI from different threads?

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

No response from AWS in android build? 0 Answers

How to call NetworkClient.Ready() ? 1 Answer

UNET - How to listen for StopBroadcastDiscovery() to finish? And can buffer be changed while BroadcastDiscovery is running? 2 Answers

Can Jobs be used to load large scenes asynchronously without freezing? 1 Answer

What is the best way to fire a network request and get response in the EditorWindow ? 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