forked from MagicBane/Server
				
			
				 3 changed files with 58 additions and 27 deletions
			
			
		| @ -0,0 +1,42 @@ | |||||||
|  | package engine.workthreads; | ||||||
|  | 
 | ||||||
|  | import engine.job.AbstractJob; | ||||||
|  | import engine.job.JobThread; | ||||||
|  | import engine.objects.PlayerCharacter; | ||||||
|  | import org.pmw.tinylog.Logger; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | import java.util.concurrent.locks.ReentrantLock; | ||||||
|  | 
 | ||||||
|  | public class ProcessUpdate implements Runnable{ | ||||||
|  |     private final ReentrantLock lock = new ReentrantLock(); | ||||||
|  |     private PlayerCharacter playerCharacter; | ||||||
|  |     public ProcessUpdate(PlayerCharacter pc){ | ||||||
|  |         this.playerCharacter = pc; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void run() { | ||||||
|  |         try { | ||||||
|  |             if (this.playerCharacter != null) { | ||||||
|  |                 if (lock.tryLock(10, TimeUnit.SECONDS)) { // Timeout to prevent deadlock
 | ||||||
|  |                     try { | ||||||
|  |                         this.playerCharacter.update(true); | ||||||
|  |                     } finally { | ||||||
|  |                         lock.unlock(); | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     Logger.warn("JobThread could not acquire lock in time, skipping job."); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             Logger.error(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void startUpdate(PlayerCharacter pc) { | ||||||
|  |         ProcessUpdate updateThread = new ProcessUpdate(pc); | ||||||
|  |         Thread thread = new Thread(updateThread); | ||||||
|  |         thread.setName(pc.getObjectUUID() + " UPDATE"); | ||||||
|  |         thread.start(); | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue