NSW were 351 for nine at the close on Tuesday with Thornley on 204 and former Test leg-spinner Stuart MacGill 16.
Thornley was on 76 when the ninth wicket fell at 200 and when he was joined by MacGill a century seemed unlikely let alone a double ton.
But MacGill hung around for two hours as the pair added an unbeaten 151 for the last wicket.
Thornely finished the day with 18 fours and nine sixes and is one six away from equalling the Australian first-class record of 10 sixes in an innings set by the late David Hookes for South Australia against NSW in 1985-86.
Thornely's chanceless innings lifted NSW from 165 for eight.
Left-arm wrist spinner Beau Casson (3-57) was the pick of WA's depleted bowling attack.
At Sydney (day one): New South Wales 351 for 9 (D. Thornely 204 not out, B. Haddin 45; B. Casson 3-57) v Western Australia.