Describe how concurrency and multi-threading are handled in Java or JavaScript, and how to prevent deadlocks.