You can do that this way:
-- write everything from your buffers to the disc!
-- Clean all buffers and caches
-- Now shrink the file to your desired size
DBCC SHRINKFILE (TEMPDEV, 40960);
-- Make sure that there is no running transaction which uses the tempdb while shrinking!
-- This is most trickiest part of it all.
The last step is the most trickiest. During the shrink process, no other action should use the tempdb, as this could cause an abort of your
SHRINKFILE operation. Due to the fact that the tempdb is quite easy to shrink, it shouldn't take to long to shrink it.
Beware that this is something like a "soft restart". Everything will be removed from the buffers and written to the disc. This means an impact on your I/O subsystem (write) as it have to handle all the write operations. After that you can shrink the file (which has an impact on read and write performance) and at the end, all processes which query any table will need to retrieve the data back from the I/O subsystem into the buffers. This may hurt more than an restart.
If you're running a development system, you should just restart the machine instead of this way. But on some production systems without a failover partner, this may be useful.