From hundreds +and +thousands. Sense 2 (“tiny balls or strands of multicoloured sugar”) is from the fact that the confectionery is made up of numerous sugar pieces.
1864 September 12, W. T. Sherman, letter to James M. Calhoun, E. E. Rawson and S. C. Wells, representing City Council of Atlanta, published in 1865, Joel Tyler Headley, Grant and Sherman: Their Campaigns and Generals, 2008, page 592,
I myself have seen in Missouri, Kentucky, Tennessee, and Mississippi, hundreds and thousands of women and children fleeing from your armies and desperadoes, hungry, and with bleeding feet.
1901, United States Congress, Congressional Edition, volume 4338, page 167:
Have there not been hundreds and thousands of people injured in the depression in value of railroad securities?
1949, A.S. Altekar, State and Government in Ancient India, reprinted 2001, page 148,
It is clear that the sentence rather refers to numerous favours conferred upon the city and country population, the monetary value of which amounted to hundreds and thousands of rupees or contemporary coins.
2003, Neil Wills, “Surfers′ Paradise”, in Third Way, page 12:
So there you have them - Onoda, Gund, Eclipse - all squidged together on the same spoon, their flavours intermingling under a bleeding crust of hundreds-and-thousands.
2007, Pippa Cuthbert, Lindsay Cameron Wilson, Cookies!, New Holland Publishers (UK), archived from the original on 17 September 2016, page 96:
Hundreds and thousands, commonly yet boringly called “sprinkles” in North America, are enough to make any child smile. Sprinkled over a cookie and sandwiched around creamy ice cream makes for sheer bliss!