5 questions per day Eight (Java Thread II)

Source: Internet
Author: User

What is the difference between runnable and callable in Java?

Both runnable and callable represent tasks that you want to perform in different threads. Runnable from the JDK1.0 began, callable is in the JDK1.5 increase. The main difference is that the callable call () method can return a value and throw an exception, while the runnable run () method does not have these features. The callable can return a future object loaded with calculated results.

What is the difference between Cyclicbarrier and Countdownlatch in Java?

Both Cyclicbarrier and Countdownlatch can be used to allow a group of threads to wait for other threads. Unlike Cyclicbarrier, Countdownlatch cannot be reused.

What is the Java memory model?

The Java memory model prescribes and directs Java programs to behave in a deterministic manner between different memory architectures, CPUs, and operating systems. It is especially important in the case of multithreading. The Java memory model guarantees that changes made by one thread can be visible to other threads, and that they are first-rate. This relationship defines rules that allow programmers to have a clearer idea of concurrent programming. For example, prior sex ensures that:
? The code within the thread can be executed in a sequential order, which is called the program order rule.
? For the same lock, an unlock operation must occur after another lock operation that occurs after the time, also known as a pipe lock rule.
? The previous write to volatile is also called a volatile variable rule before the next volatile read operation.
? Any operation within a thread must be called after the start () Call of this thread, also known as the thread initiation rule.
? A thread terminates the rule before all operations on a thread are terminated.
? The end operation of an object must also be called an object finalization rule after the object is constructed.
? Transitivity

What are the volatile variables in Java?

Volatile is a special modifier that can only be used by member variables. Multithreaded operations on member variables are transparent to other threads in the absence of a synchronization class for Java concurrency programs. The volatile variable guarantees that the next read will occur after the previous write operation, which is the volatile variable rule for the previous question.

What is thread safety? is vector a thread-safe class?

If your code is in a process where multiple threads are running at the same time, these threads may run the code at the same time. If the result of each run is the same as the single-threaded run, and the value of the other variable is the same as expected, it is thread-safe. The same instance object of a thread-safe counter class will not have a computational error if it is used by multiple threads. Obviously you can divide the collection classes into two groups, thread-safe and non-thread-safe. Vectors are thread-safe with synchronous methods, and ArrayList similar to it are not thread-safe.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

5 questions per day Eight (Java Thread II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.