001package edu.wpi.first.wpilibj.networktables2.thread;
002
003/**
004 * A simple thread manager that will run periodic threads in their own thread
005 * 
006 * @author Mitchell
007 *
008 */
009public class DefaultThreadManager implements NTThreadManager {
010        private static class PeriodicNTThread implements NTThread {
011                private final Thread thread;
012                private boolean run = true;
013                public PeriodicNTThread(final PeriodicRunnable r, String name){
014                        thread = new Thread(new Runnable() {
015                                public void run() {
016                                        try {
017                                                while(run){
018                                                        r.run();
019                                                }
020                                        } catch (InterruptedException e) {
021                                                //Terminate thread when interrupted
022                                        }
023                                }
024                        }, name);
025                        thread.start();
026                }
027                public void stop() {
028                        run = false;
029                        thread.interrupt();
030                }
031                public boolean isRunning() {
032                        return thread.isAlive();
033                }
034        }
035
036        public NTThread newBlockingPeriodicThread(final PeriodicRunnable r, String name) {
037                return new PeriodicNTThread(r, name);
038        }
039
040}