Search Results

Search found 16 results on 1 pages for 'finbarr'.

Page 1/1 | 1 

  • volatile keyword seems to be useless?

    - by Finbarr
    import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1 = new CountDownLatch(NUM_THREADS); private volatile int bar = 0; private AtomicInteger count = new AtomicInteger(0); private static final int NUM_THREADS = 25; public static void main(String[] args) { Main main = new Main(); for(int i = 0; i < NUM_THREADS; i++) new Thread(main).start(); } public void run() { int i = count.incrementAndGet(); cdl1.countDown(); try { cdl1.await(); } catch (InterruptedException e1) { e1.printStackTrace(); } bar = i; if(bar != i) System.out.println("Bar not equal to i"); else System.out.println("Bar equal to i"); } } Each Thread enters the run method and acquires a unique, thread confined, int variable i by getting a value from the AtomicInteger called count. Each Thread then awaits the CountDownLatch called cdl1 (when the last Thread reaches the latch, all Threads are released). When the latch is released each thread attempts to assign their confined i value to the shared, volatile, int called bar. I would expect every Thread except one to print out "Bar not equal to i", but every Thread prints "Bar equal to i". Eh, wtf does volatile actually do if not this?

    Read the article

  • Programming Language Choices for High Integrity Systems

    - by Finbarr
    What programming languages are a good choice for High Integrity Systems? An example of a bad choice is Java as there is a considerable amount of code that is inaccessible to the programmer. I am looking for examples of strongly typed, block structured languages where the programmer is responsible for 100% of the code, and there is as little interference from things like a JVM as possible. Compilers will obviously be an issue. Language must have a complete and unambiguous definition.

    Read the article

  • What is the difference between the add and offer methods in a queue?

    - by Finbarr
    Take the PriorityQueue for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E) According to the Collection API entry http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html the add method will often seek to ensure that an element exists within the Collection rather than adding duplicates. So my question is, what is the difference between the add and offer methods? Is it that the Offer method will add duplicates regardless? (I doubt that it is because if a Collection should only have distinct elements this would circumvent that).

    Read the article

  • Java return to position N in call stack.

    - by Finbarr
    Using the return keyword in Java code will return execution to the last piece of calling code in the call stack. If object foo calls baz.bar(), the return keyword in the bar method will continue code execution in foo. Let's say I have object foo that calls foofoo that calls foofoofoo in the above scenario, and foofoofoo calls baz.bar(). Is there anyway in Java to use the return keyword, or something else, so that the code in the bar method can return all the way back up to foo? (WITHOUT THROWING AN EXCEPTION)

    Read the article

  • JDOQL Any way to avoid multiple .contains() calls in the query when searching for the presence of on

    - by Finbarr
    The question pretty much says it all. If I have a class Class A public class A { ... private List<String> keys; ... } And I want to select all A instances from the DataStore that have atleast one of a List of keys, is there a better way of doing it than this: query = pm.newQuery(A.class); query.setFilter("keys.contains(:key1) || keys.contains(:key2) || keys.contains(:key3)"); List<A> results = (List<A>)query.execute(key1, key2, key3); This has not yet been implemented, so I am open to radical suggestions.

    Read the article

  • What is the effect of final variable declaration in methods?

    - by Finbarr
    Classic example of a simple server: class ThreadPerTaskSocketServer { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handleRequest(connection); } }; new Thread(task).start(); } } } Why should the Socket be declared as final? Is it because the new Thread that handles the request could refer back to the socket variable in the method and cause some sort of ConcurrentModificationException?

    Read the article

  • Is this technically thread safe despite being mutable?

    - by Finbarr
    Yes, the private member variable bar should be final right? But actually, in this instance, it is an atomic operation to simply read the value of an int. So is this technically thread safe? class foo { private int bar; public foo(int bar) { this.bar = bar; } public int getBar() { return bar; } } // assume infinite number of threads repeatedly calling getBar on the same instance of foo.

    Read the article

  • What is wrong with this SQL syntax?

    - by Finbarr
    UPDATE files SET filepath = REPLACE(filepath, `sites/somedomain.com/files/`, `sites/someotherdomain.com/files/`); I have a table called files with a field called filepath. MySQL returns this error: Unknown column 'sites/somedomain.com/files/' in 'field list'

    Read the article

  • MySQL One to Many order by number found in joined table

    - by Finbarr
    Two tables: table_a ------- table_a_id: (primary, int) table_b ------- table_a_id: (index, int, from table_a) table_b_value: (varchar) A one to many relationship between table_a_id and table_b_value. Given a query like this: SELECT DISTINCT(table_a_id) FROM table_a JOIN table_b ON table_a.table_a_id=table_b.table_a_id I want to order by the number of occurrences of table_a_id in table_b. I'm not really sure how to write this in MySQL.

    Read the article

1