@@ -264,6 +264,7 @@ eat. Here’s the next version:
264264
265265``` rust
266266use std :: thread;
267+ use std :: time :: Duration ;
267268
268269struct Philosopher {
269270 name : String ,
@@ -279,7 +280,7 @@ impl Philosopher {
279280 fn eat (& self ) {
280281 println! (" {} is eating." , self . name);
281282
282- thread :: sleep_ms ( 1000 );
283+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
283284
284285 println! (" {} is done eating." , self . name);
285286 }
@@ -313,13 +314,13 @@ from the standard library, and so we need to `use` it.
313314 fn eat(&self) {
314315 println!("{} is eating.", self.name);
315316
316- thread::sleep_ms( 1000);
317+ thread::sleep(Duration::from_millis( 1000) );
317318
318319 println!("{} is done eating.", self.name);
319320 }
320321```
321322
322- We now print out two messages, with a ` sleep_ms() ` in the middle. This will
323+ We now print out two messages, with a ` sleep ` in the middle. This will
323324simulate the time it takes a philosopher to eat.
324325
325326If you run this program, you should see each philosopher eat in turn:
@@ -345,6 +346,7 @@ Here’s the next iteration:
345346
346347``` rust
347348use std :: thread;
349+ use std :: time :: Duration ;
348350
349351struct Philosopher {
350352 name : String ,
@@ -360,7 +362,7 @@ impl Philosopher {
360362 fn eat (& self ) {
361363 println! (" {} is eating." , self . name);
362364
363- thread :: sleep_ms ( 1000 );
365+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
364366
365367 println! (" {} is done eating." , self . name);
366368 }
@@ -493,6 +495,7 @@ Let’s modify the program to use the `Table`:
493495
494496``` rust
495497use std :: thread;
498+ use std :: time :: Duration ;
496499use std :: sync :: {Mutex , Arc };
497500
498501struct Philosopher {
@@ -512,12 +515,12 @@ impl Philosopher {
512515
513516 fn eat (& self , table : & Table ) {
514517 let _left = table . forks[self . left]. lock (). unwrap ();
515- thread :: sleep_ms ( 150 );
518+ thread :: sleep ( Duration :: from_millis ( 150 ) );
516519 let _right = table . forks[self . right]. lock (). unwrap ();
517520
518521 println! (" {} is eating." , self . name);
519522
520- thread :: sleep_ms ( 1000 );
523+ thread :: sleep ( Duration :: from_millis ( 1000 ) );
521524
522525 println! (" {} is done eating." , self . name);
523526 }
@@ -598,12 +601,12 @@ We now need to construct those `left` and `right` values, so we add them to
598601``` rust,ignore
599602fn eat(&self, table: &Table) {
600603 let _left = table.forks[self.left].lock().unwrap();
601- thread::sleep_ms( 150);
604+ thread::sleep(Duration::from_millis( 150) );
602605 let _right = table.forks[self.right].lock().unwrap();
603606
604607 println!("{} is eating.", self.name);
605608
606- thread::sleep_ms( 1000);
609+ thread::sleep(Duration::from_millis( 1000) );
607610
608611 println!("{} is done eating.", self.name);
609612}
@@ -614,8 +617,8 @@ We have three new lines. We’ve added an argument, `table`. We access the
614617the fork at that particular index. That gives us access to the ` Mutex ` at that
615618index, and we call ` lock() ` on it. If the mutex is currently being accessed by
616619someone else, we’ll block until it becomes available. We have also a call to
617- ` thread::sleep_ms ` between the moment first fork is picked and the moment the
618- second forked is picked, as the process of picking up the fork is not
620+ ` thread::sleep ` between the moment the first fork is picked and the moment the
621+ second forked is picked, as the process of picking up the fork is not
619622immediate.
620623
621624The call to ` lock() ` might fail, and if it does, we want to crash. In this
0 commit comments