Browsing 7239 questions and answers with Jon Skeet

Can I recursively call an async function without overflowing the stack?

Since the return site of the async function isn't the caller, I assume this works, but thought I'd verify that this was safe just in case. If it's not, why would this overflow the...
Jon Skeet
people
quotationmark

The reason it's working for you is not because of the way CheckAsync is called, but because you're awaiting the result of Task.Delay. That will always return a "not completed yet" task, so awaiting it will schedule a continuation. That... more 8/24/2014 8:07:34 AM

people

Stream<Set<Path>> to Set<Path>

Here is the Java 8 code, using streams: Set<String> getFields( Path xml ) { final Set<String> fields = new HashSet<>(); for( ... ) { ... ...
Jon Skeet
people
quotationmark

I suspect you want flatMap instead of map, and then use Collectors.toCollection to create the sorted set: final SortedSet<String> fields = files .stream() .parallel() .flatMap(x -> getFields(x).stream()) ... more 8/22/2014 11:17:42 PM

people

Why this Joda offset cannot get daylightsaving right

I have the following code using Joda to calculate offset for any given day, for example int offset = DateTimeZone.forID("EST").getOffset(new DateTime(2013,8,1,1,1)); this will...
Jon Skeet
people
quotationmark

You're expected to give a time zone ID - not the abbreviation for "half" a time zone. So for example, America/New_York is one example of a time zone ID for Eastern Time (there are others, for time zones which may be like New York now, but... more 8/22/2014 7:14:51 PM

people

Why does this async method lock up the user interface?

I am using the following code to read all the images on a network drive and poplate an ImageControl with each, and then display them on the screen. The problem I'm having is that...
Jon Skeet
people
quotationmark

You're using Task.WaitAll - that blocks until all the tasks have completed. Instead, you should use Task.WhenAll, which returns a Task which will itself complete when all the other tasks have completed. You can then await that. await... more 8/22/2014 4:45:05 PM

people

Getting random numbers in a thread safe way

Here is a nice article describing thread safety of random numbers:Getting random numbers in a thread-safe way But I'm stuck with the "RandomGen2" example: public static class...
Jon Skeet
people
quotationmark

Note: since writing this answer, I've become aware of issues creating multiple Random instances, even though it sounds like it should work. I've generally found that a better alternative is to have a single Random instance and just lock on... more 8/22/2014 1:25:59 PM

people

What is the difference between using .newInstance() and not using?

I see a lot of java examples connecting to a db, makes a call to newInstance(). Some don't use it at all. I tried both and are working fine. I couldn't understand why some use...
Jon Skeet
people
quotationmark

In modern Java, neither of these is needed. The reason for using Class.forName in "the good old days" was that it would run the type initialization code, which would register the driver with JDBC. You don't need to create a new instance... more 8/22/2014 9:33:22 AM

people

Randomly generating from a dictionary and returning the key and value

This is a 2 part question, I am making a blackjack game and I am trying to randomly generate a key and value (KEY = string(card value e.g. Hearts2), And VALUE = int (Score for...
Jon Skeet
people
quotationmark

There are several issues here: Dictionaries aren't ordered - so the order in which the cards come out of OrderBy may not be reflected when you iterate over the dictionary later. I believe in the current implementation it may happen to do... more 8/22/2014 9:18:51 AM

people

How to calculate milliseconds with Java as in Ruby?

I can calculate the time in milliseconds with the following Ruby snippet: $ irb > require 'date' => true > Date.new(2014,9,5).to_time => 2014-09-05 00:00:00...
Jon Skeet
people
quotationmark

There are three problems with your current code: "CEST" isn't a time zone Java recognized. Try Europe/Paris instead, as a European time zone java.util.Calendar uses 0-based months (yes, it's awful) You haven't cleared out the time part... more 8/22/2014 8:53:51 AM

people

Storing converted value in a Variable

I am trying to store the value of an extracted string..I can't get it to work using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace...
Jon Skeet
people
quotationmark

Shouldn't 3 be stored in the variable c? No, the integer representation of '3' should be stored in the variable c, and indeed is. You're effectively doing: char tmp = a[a.Length - 1]; // tmp = '3' int c = tmp; // Implicit conversion... more 8/22/2014 8:35:55 AM

people

Group by counting in Java 8 stream API

I try to find a simple way in Java 8 stream API to do the grouping, I come out with this complex way! List<String> list = new...
Jon Skeet
people
quotationmark

I think you're just looking for the overload which takes another Collector to specify what to do with each group... and then Collectors.counting() to do the counting: import java.util.*; import java.util.stream.*; class Test { public... more 8/22/2014 6:55:51 AM

people